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?
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).
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')
Try this:
Declare @dt NVARCHAR(20)
Select
@dt = REPLACE(CONVERT(CHAR(15), SalesDate, 106),' ',' - ')
FROM SalesTable
select CONVERT(NVARCHAR, SYSDATETIME(), 106) AS [DD-MON-YYYY]
or else
select REPLACE(CONVERT(NVARCHAR,GETDATE(), 106), ' ', '-')
both works fine
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With