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

有帮助吗?

解决方案

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

其他提示

you can do it in this way

SELECT CONVERT(nvarchar(20), '2012-11-22 06:50:10.000', 120)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top