Question

How do you convert a datetime with timestamp to nvarchar while dropping the microseconds and keeping the same format without using a series of concatenated datepart functions? I looked around, but solution didn't work for me. -

Convert datetime to nvarchar but keep format convert(nvarchar(20), '2012-11-22 06:50:10.000', 20) = Nov 22 2012 6:50AM

Ex. DATETIME: 2012-11-22 06:50:10.000 -> NVARCHAR: 2012-11-22 06:50:10

Was it helpful?

Solution

This seems to work for me -

declare @dt datetime = '2012-11-22 06:50:10.000'
select convert(varchar(20),@dt,120) -- result is 2012-11-22 06:50:10

And here is a SQL Fiddle showing it: http://www.sqlfiddle.com/#!3/b9bb1/7

This -

convert(nvarchar(20), '2012-11-22 06:50:10.000', 20)

does not work because the date is just a varchar

OTHER TIPS

you can do it in this way

SELECT CONVERT(nvarchar(20), '2012-11-22 06:50:10.000', 120)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top