Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 922
Saquibul Islam Waheed Avatar asked Aug 13 '13 06:08

Saquibul Islam Waheed


People also ask

How do you check if a date is in a particular format in SQL?

SQL has IsDate() function which is used to check the passed value is date or not of specified format, it returns 1(true) when the specified value is date otherwise it return 0(false).

How do I change the date format in SQL Server?

You can specify the format of the dates in your statements using CONVERT and FORMAT. For example: select convert(varchar(max), DateColumn, 13), format(DateColumn, 'dd-MMM-yyyy')


2 Answers

Try this:

Declare @dt NVARCHAR(20)

Select 
    @dt = REPLACE(CONVERT(CHAR(15), SalesDate, 106),' ',' - ') 
FROM SalesTable
like image 175
Abhishek Jain Avatar answered Oct 07 '22 07:10

Abhishek Jain


select CONVERT(NVARCHAR, SYSDATETIME(), 106) AS [DD-MON-YYYY]

or else

select REPLACE(CONVERT(NVARCHAR,GETDATE(), 106), ' ', '-')

both works fine

like image 34
abcd123 Avatar answered Oct 07 '22 08:10

abcd123