Reputation: 7019
How do I use sql to get the whole month name in sql server?
I did't find a way using DATEPART(mm, mydate)
or CONVERT(VARCHAR(12), CreatedFor, 107)
.
Basically I need in the format: April 1 2009.
Upvotes: 17
Views: 142269
Reputation: 646
Most answers are a bit more complicated than necessary, or don't provide the exact format requested.
select Format(getdate(), 'MMMM dd yyyy') --returns 'October 01 2020', note the leading zero
select Format(getdate(), 'MMMM d yyyy') --returns the desired format with out the leading zero: 'October 1 2020'
If you want a comma, as you normally would, use:
select Format(getdate(), 'MMMM d, yyyy') --returns 'October 1, 2020'
Note: even though there is only one 'd' for the day, it will become a 2 digit day when needed.
Upvotes: 3
Reputation: 1
select datename(DAY,GETDATE()) +'-'+ datename(MONTH,GETDATE()) +'- '+
datename(YEAR,GETDATE()) as 'yourcolumnname'
Upvotes: 0
Reputation: 1563
If you are using SQL Server 2012 or later, you can use:
SELECT FORMAT(MyDate, 'MMMM dd yyyy')
You can view the documentation for more information on the format.
Upvotes: 21
Reputation: 3800
109 - mon dd yyyy (In SQL conversion)
The required format is April 1 2009
so
SELECT DATENAME(MONTH, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 109), 9)
Result is:
Upvotes: 1
Reputation: 69514
SELECT DATENAME(MONTH, GETDATE())
+ RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
OR Date without Comma Between date and year, you can use the following
SELECT DATENAME(MONTH, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2))
+ ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month DD YYYY]
Upvotes: 31