需求如下:
我要将
20110810081703
20110810081830
转化为:
2011-08-10 08:17:03
2011-08-10 08:18:30
先创建测试数据:
create table #T1 ([col] varchar(20))
insert into #T1
select 20110810081703 union all
select 20110810081830 union all
select 20110810144626 union all
select 20110810092209 union all
select 20110810090558 union all
select 20110810090600 union all
select 20110810141857 union all
select 20110810141858 union all
select 20110810092837
方法一:利用
stuff函数select stuff(stuff(stuff(stuff(stuff(col,5,0,'-'),8,0,'-'),11,0,' '),14,0,':'),17,0,':')+'.000' col
from #T1
方法二:利用substring函数
select convert(varchar(10),cast(left(col ,8) as datetime),120)+' '
+substring(col ,9,2)+':'+substring(col ,11,2)+':'+right(col ,2) as col
from #T1
转换方法还有很多种,自己慢慢摸索吧!