2014-12-05 115 views
-1

我有点生疏,似乎完全忘记了如何做到这一点。我正在接收数据,datatime列采用字符串格式char(12)SQL Server查询(查看)将字符串转换为DateTime

例子:201411061900

我将如何编写查询(视图)将其转换成DateTime

我用CONVERT (datetime, dbo.KWH.mr_dtm,120),但我得到的错误Conversion failed when converting date and/or time from character string.

我没有多少运气。非常感谢你提前。

如果你想建议-5时区也,我不介意。 :)

回答

0

Magnus回答了我的问题。我希望在这里发布它,以帮助任何人在未来。 Here is the link to the answer.

没有输出格式标识符(例如120),它允许您将字符(12)转换为SQL Server中的日期时间。但你可以使用Substring函数:

Select Cast(Substring(dbo.KWH.mr_dtm,1,8) + ' ' + Substring(dbo.KWH.mr_dtm,9,2)+':'+ Substring(dbo.KWH.mr_dtm,11,2) as DateTime) 

谢谢大家!这是非常赞赏。
哦,这篇文章帮助我解决我的时区设置。

Convert Datetime column from UTC to local time in select statement

非常感谢Michael Goldshteyn的时区转换。