Convert datetime to nvarchar but keep format

I return either DATETIME or NVARCHAR = 'MULTIPLE' depending on whether the action was performed more than once.

So, I am trying to save DATETIME in the normal format '2012-10-23 13: 59: 47.000', but as NVARCHAR. SQL wants to do this "October 23, 2012 12:40 PM" How can I do this?

Now I am doing:

CAST(r.Date_And_Time) AS NVARCHAR(30)) 
+7
source share
2 answers
 Declare @CreatedDate datetime Select @CreatedDate='20121210' Select CONVERT(VARCHAR,@createdDate, 21) 
+10
source

Use CONVERT . It has a format option.

 CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) CONVERT(NVARCHAR(23), r.Date_And_Time, 121) 

https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

+9
source

All Articles