我需要从yyyymmddhhmmss计算当地时间,并将其返回为yyyymmddhhmmss。我已经尝试了下面,它正在工作,但我无法摆脱月份名称。在sql server中将datetime转换为yyyymmddhhmmss
Declare @VarCharDate varchar(max)
Declare @VarCharDate1 varchar(max)
Declare @VarCharDate2 varchar(max)
--Declare
set @VarCharDate = '20131020215735' --- YYYYMMDDHHMMSS
--Convert
set @VarCharDate1 =(select SUBSTRING(@VarCharDate,0,5) + '/' + SUBSTRING(@VarCharDate,5,2) + '/' + SUBSTRING(@VarCharDate,7,2) + ' ' + SUBSTRING(@VarCharDate,9,2) +':'+SUBSTRING(@VarCharDate,11,2) +':' + RIGHT(@VarCharDate,2))
select @VarCharDate1
--Convert to Date and Add offset
set @VarCharDate2 = DATEADD(HOUR,DateDiff(HOUR, GETUTCDATE(),GETDATE()),CONVERT(DATETIME,@VarCharDate1,20))
select @VarCharDate2
-- Now we need to revert it to YYYYMMDDhhmmss
--Tried this but month name still coming
Select convert(datetime, @VarCharDate2, 120)
关于“从yyyymmddhhmmss返回为yyyymmddhhmmss”,这两种格式对我来说看起来是一样的。无论如何,如果你开始使用字符串,顺便说一句坏主意,将它转换为日期时间,然后返回到所需格式的字符串。不需要子字符串。 –