我需要從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」,這兩種格式對我來說看起來是一樣的。無論如何,如果你開始使用字符串,順便說一句壞主意,將它轉換爲日期時間,然後返回到所需格式的字符串。不需要子字符串。 –