34,588
社区成员
发帖
与我相关
我的任务
分享
create table tb(col varchar(20))
insert into tb values('2012-03-18')
insert into tb values('20120318')
insert into tb values('201203181513')
insert into tb values('2012-03-18 15:13')
go
select col,col_time = (case when len(col) > 8 and charindex('-',col) = 0
then cast(substring(col,1,4) + '-' + substring(col,5,2) + '-' + substring(col,7,2) + ' ' + substring(col,9,2) + ':' + substring(col,11,2) as datetime)
else cast(col as datetime)
end)
from tb
drop table tb
/*
col col_time
-------------------- ------------------------------------------------------
2012-03-18 2012-03-18 00:00:00.000
20120318 2012-03-18 00:00:00.000
201203181513 2012-03-18 15:13:00.000
2012-03-18 15:13 2012-03-18 15:13:00.000
(所影响的行数为 4 行)
*/
cast(col as datetime)