How can I get the month number (not month name) from a date in SQL Server?

前端 未结 7 1595
孤街浪徒
孤街浪徒 2021-02-02 08:37

How can I get the month number in sql? I use the following code but it returns the month name.

SELECT DATENAME(mm, GETDATE())
7条回答
  •  面向向阳花
    2021-02-02 09:02

    You can also use this to pad the month number

    SELECT RIGHT('00' + RTRIM( CAST( DATEPART( MONTH, GETDATE() ) AS varchar(2)) ) , 2)
    

提交回复
热议问题