dLight dLight - 7 months ago 28
SQL Question

Datetime Format in Month and Year

I want to format a datetime column like so: "March 2004." Currently, I tried

DECLARE @Date VARCHAR(20) = '2004-03-05 01:00'

SELECT CONVERT(VARCHAR(20),CAST(@Date AS DATETIME),13) AS DateFormats


but not getting the right result.

Answer

Something like:

DECLARE @Date VARCHAR(20) = '2004-03-05 01:00'
SELECT DATENAME(MONTH, @Date) + ' ' + DATENAME(YEAR, @Date)

More on DATENAME can be found here https://msdn.microsoft.com/en-gb/library/ms174395.aspx

Essentially it gets the month part of the date and the year part of the date and concatenate them together.