在sql server中将日期时间转换为yyyymmddhhmmss
Posted
技术标签:
【中文标题】在sql server中将日期时间转换为yyyymmddhhmmss【英文标题】:Convert datetime to yyyymmddhhmmss in sql server 【发布时间】:2013-10-30 15:15:55 【问题描述】:我需要从 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)
【问题讨论】:
关于“来自 yyyymmddhhmmss 并将其返回为 yyyymmddhhmmss”,这两种格式在我看来是一样的。无论如何,如果您从字符串开始,顺便说一句,这是个坏主意,请将其转换为日期时间,然后再转换为所需格式的字符串。不需要子字符串。 【参考方案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)
通过使用日期时间数据类型,您将始终拥有正确的日期时间
【讨论】:
【参考方案2】: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]
我认为在处理日期列时,我们还需要对不良数据进行补偿。添加了额外的验证步骤和清理代码
【讨论】:
完美。正如我在片场所做的那样,我只是使用了“STUFF”位,这可以将不是我制作的 YYYYHHDDHHmmss 字符串转换为可以在 DATEDIFF 中使用的 YYYY-MM-DD hh:mm:ss 日期时间。 【参考方案3】:试试这个 -
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
【讨论】:
以上是关于在sql server中将日期时间转换为yyyymmddhhmmss的主要内容,如果未能解决你的问题,请参考以下文章
在 SQL Server 数据库中将长日期时间转换为实际日期时间的最佳方法是啥?