2013-10-21 176 views
1

我需要從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) 
+0

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

回答

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

使用日期時間數據類型,你會永遠擁有正確的日期時間

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] 

我想在處理日期列時,我們還需要補償不良數據。添加了額外的驗證步驟和清理代碼

相關問題