首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

类型转换解决方法

2012-02-02 
类型转换tabidtime121-03-07202-01-06.............这张表中time为string,现在我想将其改为datetime型,记

类型转换
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 行)

*/

热点排行
Bad Request.