Convert month name to month number in SQL Server

后端 未结 13 1333
情话喂你
情话喂你 2020-12-01 13:57

In T-SQL what is the best way to convert a month name into a number?

E.g:

\'January\' -> 1
\'February\' -> 2
\'March\' -> 3
13条回答
  •  情深已故
    2020-12-01 15:02

    You can use below code

    DECLARE @T TABLE ([Month] VARCHAR(20))
    INSERT INTO @T
    SELECT 'January'
    UNION
    SELECT 'February'
    UNION
    SELECT 'March'`
    
    SELECT MONTH('01-' + [Month] + '-2010') As MonthNumeric,[Month] FROM @T
    ORDER BY MonthNumeric
    

提交回复
热议问题