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 convert(varchar(11), transfer_date, 106)
got me my desired result of date formatted as 07 Mar 2018
My column transfer_date
is a datetime type column and I am using SQL Server 2017 on azure
Try this:
Declare @dt NVARCHAR(20)
Select
@dt = REPLACE(CONVERT(CHAR(15), SalesDate, 106),' ',' - ')
FROM SalesTable
I Think this is the best way to do it.
REPLACE(CONVERT(NVARCHAR,CAST(WeekEnding AS DATETIME), 106), ' ', '-')
Because you do not have to use varchar(11)
or varchar(10)
that can make problem in future.
It doesn't look like DD-MMM-YYYY is supported by default (at least, with dash as separator). However, using the AS clause, you should be able to do something like:
SELECT CONVERT(VARCHAR(11), SYSDATETIME(), 106) AS [DD-MON-YYYY]
See here: http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
SELECT Convert(varchar(10),CONVERT(date,'columnname',105),105) as "end";
OR
SELECT CONVERT(VARCHAR(10), CAST(event_enddate AS DATE), 105) AS [end];
will return the particular date in the format of 'dd-mm-yyyy'
The result would be like this..
04-07-2016
select CONVERT(NVARCHAR, SYSDATETIME(), 106) AS [DD-MON-YYYY]
or else
select REPLACE(CONVERT(NVARCHAR,GETDATE(), 106), ' ', '-')
both works fine