문제

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)
도움이 되었습니까?

해결책 3

Try this -

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]

It will return -

date
20131021035700

다른 팁

    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)

by using datetime data type you will always have the correct datetime

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]

I think when dealing with Date columns, we need to also be compensating for bad data. Added additional Validation Steps and cleaned-up code

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top