How can I get the month number (not month name) fr

2020-05-24 20:06发布

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条回答
叼着烟拽天下
2楼-- · 2020-05-24 20:37

This will return with two char in case of Jan-Sep:

SELECT CASE WHEN LEN(MONTH(GETDATE())) = 1 THEN '0' + CAST(MONTH(GETDATE()) AS VARCHAR(2)) 
WHEN LEN(MONTH(GETDATE())) = 2 THEN CAST(MONTH(GETDATE()) AS VARCHAR(2)) END
查看更多
Viruses.
3楼-- · 2020-05-24 20:38

Use the month function - SELECT MONTH(GETDATE())

查看更多
我只想做你的唯一
4楼-- · 2020-05-24 20:46

You can also use this to pad the month number

SELECT RIGHT('00' + RTRIM( CAST( DATEPART( MONTH, GETDATE() ) AS varchar(2)) ) , 2)
查看更多
手持菜刀,她持情操
5楼-- · 2020-05-24 20:49

You want DATEPART:

select datepart(mm, getdate())
查看更多
冷血范
6楼-- · 2020-05-24 20:52

Use datepart function with m extension.

SELECT DATEPART(m, getdate())
查看更多
可以哭但决不认输i
7楼-- · 2020-05-24 20:55

Try the below:

SELECT DATEPART(mm,getdate())
查看更多
登录 后发表回答