当前位置: 代码迷 >> Sql Server >> 如何将字符串‘20150302123426000’转换为日期?
  详细解决方案

如何将字符串‘20150302123426000’转换为日期?

热度:90   发布时间:2016-04-24 09:42:29.0
怎么将字符串‘20150302123426000’转换为日期????
如:2014-11-17 08:50:59.803这样的格式
------解决思路----------------------
DECLARE @STR VARCHAR(20)
SET @STR='20150302123426000'
SELECT CAST(LEFT(@STR,8)+' '+SUBSTRING(@STR,9,2)+':'+SUBSTRING(@STR,11,2)+':'+SUBSTRING(@STR,13,2)+'.'+SUBSTRING(@STR,15,3)AS DATETIME)
直接字符串处理,或者写个函数包装处理了下
------解决思路----------------------
你这个只能这样了:

select left('20150302123426000',4)+'-'+substring('20150302123426000',5,2)+'-'
+substring('20150302123426000',7,2)+' '+substring('20150302123426000',9,2)+':'+substring('20150302123426000',11,2)+':'
+substring('20150302123426000',13,2)+'.'+substring('20150302123426000',15,3)