Convert month name to month number in SQL Server

后端 未结 13 1231
情话喂你
情话喂你 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 14:37

    You can do it this way, if you have the date (e.g. SubmittedDate)

    DATENAME(MONTH,DATEADD(MONTH, MONTH(SubmittedDate) - 1, 0)) AS ColumnDisplayMonth
    

    Or you can do it this way, if you have the month as an int

    DATENAME(MONTH,DATEADD(MONTH, @monthInt - 1, 0)) AS ColumnDisplayMonth
    
    0 讨论(0)
  • 2020-12-01 14:38

    You can create a function and then refer to it in the select statement. The function may look similar to this:

    if OBJECT_ID('fn_month_name_to_number', 'IF') is not null
    drop function fn_month_name_to_number
    go
    create function fn_month_name_to_number (@monthname varchar(25))
    returns int as
    begin
    declare @monthno as int;
    select @monthno =
    case @monthname
    when 'January' then 1
    when 'February' then 2
    when 'March' then 3
    when 'April' then 4
    when 'May' then 5
    when 'June' then 6
    when 'July' then 7
    when 'August' then 8
    when 'September' then 9
    when 'October' then 10
    when 'November' then 11
    when 'December' then 12
    end
    return @monthno
    end
    

    Then you can query it.

    select fn_month_name_to_number ('February') as month_no
    

    This query will return 2 as month number. You can pass values from a column as parameters to the function.

    select fn_month_name_to_number (*columnname*) as month_no from *tablename*
    

    Have a good day!

    0 讨论(0)
  • 2020-12-01 14:39
    SELECT DATEPART(MM,'january '+'01 1900')
    SELECT MONTH('january ' + '01 1900')
    SELECT month(dateadd(month,DATEDIFF(month,0,'january 01 2015'),0))
    
    0 讨论(0)
  • 2020-12-01 14:40

    There is no built in function for this.

    You could use a CASE statement:

    CASE WHEN MonthName= 'January' THEN 1
         WHEN MonthName = 'February' THEN 2
         ...
         WHEN MonthName = 'December' TNEN 12
    END AS MonthNumber 
    

    or create a lookup table to join against

    CREATE TABLE Months (
        MonthName VARCHAR(20),
        MonthNumber INT
    );
    
    INSERT INTO Months
        (MonthName, MonthNumber)
        SELECT 'January', 1
        UNION ALL
        SELECT 'February', 2
        UNION ALL
        ...
        SELECT 'December', 12;
    
    SELECT t.MonthName, m.MonthNumber
        FROM YourTable t
            INNER JOIN Months m
                ON t.MonthName = m.MonthName;
    
    0 讨论(0)
  • 2020-12-01 14:40

    I recently had a similar experience (sql server 2012). I did not have the luxury of controlling the input, I just had a requirement to report on it. Luckily the dates were entered with leading 3 character alpha month abbreviations, so this made it simple & quick:

    TRY_CONVERT(DATETIME,REPLACE(obs.DateValueText,SUBSTRING(obs.DateValueText,1,3),CHARINDEX(SUBSTRING(obs.DateValueText,1,3),'...JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC')/4)) 
    

    It worked for 12 hour:
    Feb-14-2015 5:00:00 PM 2015-02-14 17:00:00.000
    and 24 hour times:
    Sep-27-2013 22:45 2013-09-27 22:45:00.000

    (thanks ryanyuyu)

    0 讨论(0)
  • 2020-12-01 14:42

    You can try sth like this, if you have month_name which is string datetype.After converting, you can feel free to order by Month.

    For example, your table like this:

     month
     Dec
     Jan
     Feb
     Nov
     Mar
      .
      .
      .
    

    My syntax is:

     Month(cast(month+'1 2016' as datetime))
    
    0 讨论(0)
提交回复
热议问题