Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

SELECT DATENAME(mm, GETDATE())
like image 991
mjyazdani Avatar asked Feb 05 '13 09:02

mjyazdani


People also ask

How do I get the number of months in SQL?

The MONTH() function returns the month part for a specified date (a number from 1 to 12).

How do I get the month from a date in SQL?

Here's how this works: First we format the date in YYYYMMDD... format truncating to keep just the 6 leftmost characters in order to keep just the YYYYMM portion, and then append '01' as the month - and voila! you have the first day of the current month.

How do I get full month name in SQL?

MySQL MONTHNAME() Function The MONTHNAME() function returns the name of the month for a given date.


3 Answers

Use datepart function with m extension.

SELECT DATEPART(m, getdate())
like image 165
Vishwanath Dalvi Avatar answered Oct 28 '22 00:10

Vishwanath Dalvi


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

like image 41
Paul J Avatar answered Oct 28 '22 00:10

Paul J


Use Datepart:

DATEPART(mm,getdate());
like image 4
CloudyMarble Avatar answered Oct 28 '22 01:10

CloudyMarble