在sql server中将datetime转换为yyyymmddhhmmss

编程入门 行业动态 更新时间:2024-10-25 11:20:43
本文介绍了在sql server中将datetime转换为yyyymmddhhmmss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要从yyyymmddhhmmss计算当地时间,并将其作为yyyymmddhhmmss返回。我尝试过下面的工作,但是我无法摆脱月份名称。

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)

推荐答案

尝试这个 -

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中将datetime转换为yyyymmddhhmmss

本文发布于:2023-11-04 12:15:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   中将   server   sql   datetime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!