문제

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