问一个时间转化的菜鸟问题
比如我一个字段是char类型,存的是 "20070203145114 "这样的,我写select怎么把他查出来是2007-02-03 14:51:14这样的,而且是时间类型啊,这个select怎么写啊?
[解决办法]
create table T(col varchar(30))
insert T select '20070203145114 '
select col=stuff(stuff(stuff(stuff(stuff(col, 5, 0, '- '), 8, 0, '- '), 11, 0, ' '), 14, 0, ': '), 17, 0, ': ') from T
--result
col
-----------------------------------
2007-02-03 14:51:14
(1 row(s) affected)
[解决办法]
declare @vart varchar(1000)
set @vart= '20070203145114 '
set @vart=stuff(@vart,5,0, '- ')
set @vart=stuff(@vart,8,0, '- ')
set @vart=stuff(@vart,11,0, ' ')
set @vart=stuff(@vart,14,0, ': ')
set @vart=stuff(@vart,17,0, ': ')
select cast(@vart as datetime) as time
你写成自定义函数就可以了