将datetime转换为sql server中的yyyymmddhhmmss

时间:2013-10-21 22:50:13

标签: sql sql-server datetime gmt

我需要从yyyymmddhhmmss计算当地时间并将其作为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)

3 个答案:

答案 0 :(得分: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)

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

答案 1 :(得分: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

答案 2 :(得分:0)

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]

我认为在处理Date列时,我们还需要补偿不良数据。添加了其他验证步骤和清理代码