1

I need to calculate the local time from yyyymmddhhmmss and return it as yyyymmddhhmmss. I have tried the below, it is working but I am not able to get rid of the month name.

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)
4

3 回答 3

1
    Declare  @VarCharDate  varchar(max)
Declare  @VarCharDate1 varchar(max)
Declare  @VarCharDate2 datetime


--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,120))
select @VarCharDate2   


-- Now we need to revert it to YYYYMMDDhhmmss
--Tried this but month name still coming
Select convert(datetime, @VarCharDate2, 120)

通过使用日期时间数据类型,您将始终拥有正确的日期时间

于 2013-10-21T23:04:35.833 回答
1
DECLARE  @VarcharDate  VARCHAR(14)
DECLARE  @VarcharDateWorker VARCHAR(19)
DECLARE  @VarcharDateResult VARCHAR(19)

--DECLARE
SET  @VarcharDate = '20131020215735' --- YYYYMMDDHHMMSS
SELECT @VarcharDate AS [InputValue]

--Convert String to date format. Adding trailing space to ensure STUFF can validate the string (Length 19 else NULL)
SET @VarcharDateWorker = STUFF(STUFF(STUFF(STUFF(STUFF(STUFF(@VarcharDate+' ',5,0,'-'),8,0,'-'),11,0,' '),14,0,':'),17,0,':'),20,0,'')
SELECT @VarcharDateWorker AS [TransformStage1]

--Check if date is valid (Return Null if date is invalid)
SET @VarcharDateWorker = CASE WHEN ISDATE(@VarcharDateWorker) = 1 THEN @VarcharDateWorker ELSE NULL END
SELECT @VarcharDateWorker AS [TransformStage2]

--Convert to Date and Add offset
SET @VarcharDateWorker =  CONVERT(VARCHAR(19),DATEADD(HOUR,DateDiff(HOUR, GETUTCDATE(),GETDATE()),@VarcharDateWorker),120)
SELECT @VarcharDateWorker   AS [TransformStage3]

--Cleanout Special Characters to get YYYYMMDDHHMMSS format
SET @VarcharDateResult =  REPLACE(REPLACE(REPLACE(@VarcharDateWorker, ' ', ''), '-', ''), ':', '')
SELECT @VarcharDateResult   AS [OutputValue]

我认为在处理日期列时,我们还需要补偿不良数据。添加了额外的验证步骤和清理代码

于 2015-09-26T01:51:56.677 回答
0

尝试这个 -

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   

SELECT REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19), CONVERT(DATETIME, @VarCharDate2, 112), 126), '-', ''), 'T', ''), ':', '') [date]

它会回来——

date
20131021035700
于 2013-10-21T23:53:35.340 回答