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

2020-05-24 20:11发布

问题:

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

SELECT DATENAME(mm, GETDATE())

回答1:

Use datepart function with m extension.

SELECT DATEPART(m, getdate())


回答2:

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



回答3:

Use Datepart:

DATEPART(mm,getdate());


回答4:

You want DATEPART:

select datepart(mm, getdate())


回答5:

Try the below:

SELECT DATEPART(mm,getdate())


回答6:

You can also use this to pad the month number

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


回答7:

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