Search code examples
sql-serversql-server-2012

Format date as 'DD Month YYYY'


Just have a mssql query that has a date in the format:

'2016-03-22 00:00:00.000' 

I need to format it as:

'22 March 2016'

I'm using SQL Server 2012. I've tried googling and the usual 106, 112 codes don't seem to work.

Is there a specific code format I can use?


Solution

  • Try the FORMAT function:

    SELECT FORMAT(GETDATE(), 'D', 'en-gb')
    

    If your version does not support the FORMAT function, you can do it by concatenating the date parts:

    SELECT 
        RIGHT('00' + CAST(DATEPART(DAY, GETDATE()) AS VARCHAR(2)), 2) + ' ' +
        DATENAME(MONTH, GETDATE()) + ' ' +
        CAST(DATEPART(YEAR, GETDATE())  AS VARCHAR(4))