Convert month name to month number in SQL Server

后端 未结 13 1344
情话喂你
情话喂你 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: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))
    

提交回复
热议问题