类型转换
tab
id time
1 21-03-07
2 02-01-06
.............
这张表中time为string,现在我想将其改为datetime型,记录样式基本不变,原纪录为dd-mm-yy,给为datetime后基本还为此样式,顺序可以改变!
谢谢大家支招!
------解决方案--------------------if object_id( 'pubs..tb ') is not null
drop table tb
go
create table tb(id int,time varchar(10),time1 datetime)
insert into tb(id,time) values(1, '21-03-07 ')
insert into tb(id,time) values(2, '02-01-06 ')
go
update tb
set time1 = cast(substring(time,7,2) + '- ' + substring(time,4,2) + '- ' + substring(time,1,2) as datetime)
select * from tb
drop table tb
/*
id time time1
----------- ---------- ------------------------------------------------------
1 21-03-07 2007-03-21 00:00:00.000
2 02-01-06 2006-01-02 00:00:00.000
(所影响的行数为 2 行)
*/