How to get a particular date format ('dd-MMM-yyyy') in SELECT query SQL Server 2008 R2

Saquibul Islam Waheed picture Saquibul Islam Waheed · Aug 13, 2013 · Viewed 176k times · Source

I am using CONVERT(data_type(length),expression,style) function to change the format of a date in a SELECT query.

Declare @dt nvarchar(20)

Select @dt = Convert(nvarchar(20), SalesDate, 113) FROM SalesTable

The format I need is 'dd-MMM-yyyy' (eg. '05-Jul-2013') but I could not find the proper style number (eg. 113) for this particular format. Can any one help me with that please?

Answer

Abhishek Jain picture Abhishek Jain · Aug 13, 2013

Try this:

Declare @dt NVARCHAR(20)

Select 
    @dt = REPLACE(CONVERT(CHAR(15), SalesDate, 106),' ',' - ') 
FROM SalesTable