在Sql-Server中按月命名

时间:2016-04-22 07:06:36

标签: sql sql-server tsql sql-order-by

我在下面的表名为session

Select SessionID, SessionName 
From dbo.Session  

SessionID   SessionName
100         August
101         September
102         October
103         November
104         December
105         January
106         May
107         June
108         July

我执行了以下查询,得到了如下输出。

SessionID   SessionName 
106         May
107         June
108         July
100         August
101         September
102         October
103         November
104         December
105         January

结果按会话ID排序。但我需要输出如下,

auth.authenticationProvider(ldapAuthenticationProvider)
    .ldapAuthentication()
    .ldapAuthoritiesPopulator(ldapAuthoritiesPopulator)
    .userSearchBase("ou=users")
    .userSearchFilter("(uid={0})")
    .groupSearchBase("ou=roles")
    .groupSearchFilter("(member={0})")
    .groupRoleAttribute("cn")
    .contextSource(contextSource);

如何在sql-server中实现这一点?谢谢你的帮助

6 个答案:

答案 0 :(得分:2)

为了避免任何具有文化依赖性的hassel,您可能会使用如下查询从sys语言中获取月份索引:

(我最终会从中创建一个TVF并传递langid作为参数)

SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) MyMonthIndex
        ,Mnth.value('.','varchar(100)') AS MyMonthName
FROM
(
    SELECT CAST('<x>' + REPLACE(months,',','</x><x>') + '</x>' AS XML) AS XmlData
    FROM sys.syslanguages
    WHERE langid=0
) AS DataSource
CROSS APPLY DataSource.XmlData.nodes('/x') AS The(Mnth)

结果

1   January
2   February
3   March
4   April
5   May
6   June
7   July
8   August
9   September
10  October
11  November
12  December

编辑:直接使用的UDF(例如在order by

CREATE FUNCTION dbo.GetMonthIndexFromMonthName(@MonthName VARCHAR(100),@langId INT)
RETURNS INT
AS
BEGIN
    RETURN
    (
        SELECT MyMonthIndex
        FROM
        (
            SELECT CAST(ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS INT) MyMonthIndex
                  ,Mnth.value('.','varchar(100)') AS MyMonthName
            FROM
            (
                SELECT CAST('<x>' + REPLACE(months,',','</x><x>') + '</x>' AS XML) AS XmlData
                FROM sys.syslanguages
                WHERE langid=@langId
            ) AS DataSource
            CROSS APPLY DataSource.XmlData.nodes('/x') AS The(Mnth)
        ) AS tbl
        WHERE MyMonthName=@MonthName
    );
END
GO

SELECT dbo.GetMonthIndexFromMonthName('February',0)

答案 1 :(得分:1)

我会使用case表达式,例如:

order by case SessionName when 'August' then 1
                          when 'September' then 2
                          ...
                          when 'Juty' then 12
         end

8月有1,因为“在应用程序逻辑中会话以8月开始”,如果你想从1月开始到12月结束,很容易重新编号。

答案 2 :(得分:1)

您可以将字符串月份转换为数字并按数字形式月份排序

SELECT SessionID, SessionName, DATEPART(MM, SessionName)
FROM dbo.Session
ORDER BY DATEPART(MM, SessionName)

否则,如果您有类似“DateCreation”列的内容,您可以这样做

SELECT SessionID, SessionName, MONTH(DateCreation)
FROM dbo.Session
ORDER BY MONTH(DateCreation)

答案 3 :(得分:0)

我认为你可以这样做:

SELECT * FROM session 
ORDER BY MONTH(session.SessionName + ' 1 2014') 

部分:

MONTH(session.SessionName + ' 1 2014')

这个月的3月份将返回3月份你是否真的需要关心这一年

答案 4 :(得分:0)

尝试CAST您的月份名称(SessionName)为DATETIME格式,就像这样

SELECT * FROM table
ORDER BY DATEPART(mm, CAST(SessionName + '1900' AS DATETIME)) asc

答案 5 :(得分:0)

尝试使用此..我假设您希望下个月的当前日期是起点,因此我使用&#34;月(GetDate()&#34;在脚本中,否则您可以硬编码到4得到理想的结果

Declare @Session Table (SessionID INT, SessionName Varchar(20))
Insert Into @Session Values (100,'August'),(101,'September'),(102,'October'),(103,'November'),(104,'December')
,(105,'January'),(106,'May'),(107,'June'),(108,'July')


Select * From @Session
        ORDER BY CASE WHEN Month(CAST('01-' + SessionName + ' 2016' AS DateTime)) - Month(GetDate()) <= 0 
                    THEN 12 + Month(CAST('01-' + SessionName + ' 2016' AS DateTime)) - Month(GetDate())
                    ELSE Month(CAST('01-' + SessionName + ' 2016' AS DateTime)) - Month(GetDate())  END      

对于德语语言..使用下面的脚本..在这个我用soundex进行月比较

SET LANGUAGE GERMAN; 
Declare @Session Table (SessionID INT, SessionName Varchar(20))
Insert Into @Session Values (100,'August'),(101,'September'),(102,'October'),(103,'November'),(104,'December')
,(105,'January'),(106,'May'),(107,'June'),(108,'July')


Select * From @Session S
        INNER JOIN 
        (SELECT Number + 1 as [MonthNumber],
                DateName(mm,DATEADD(mm,Number,0)) as [MonthName]
                FROM master..spt_values
                WHERE Type = 'P' and Number < 12
        )M ON SoundEx(M.MonthName)=SoundEx(S.SessionName)   

        ORDER BY CASE WHEN [MonthNumber] - Month(GetDate()) <= 0 
                    THEN 12 + [MonthNumber] - Month(GetDate())
                    ELSE [MonthNumber] - Month(GetDate())  END