日期:2014-05-19  浏览次数:20496 次

急...这个字段该如何更新?
mssql2000数据库里面有一个字段用来存放日期的,一开始使用字符来存.比如     "20070106 "
现在要改成日期型的     "2007-01-06 "     数据库里已经有好多数据了,该如何用SQL语句来更新?谢谢

------解决方案--------------------
只是显示的时候改变

select convert(char(10),cast(存放日期字段 as datetime),120) from 表
------解决方案--------------------
1.create一个你想要的表 a
2.
insert a
select rq=left(原来字段,4)+ '- '+substring(原来字段,5,2)+ '- '+substring(原来字段,7,2)
,其他字段 from 原来的表
3. exec sp_rename 原来的表,原来的表_copy
4. exec sp_reaname a ,原来的表
------解决方案--------------------
select convert(datetime, '20070106 ',110)


------------------------------------------------------
2007-01-06 00:00:00.000

(所影响的行数为 1 行)

------解决方案--------------------
alter table alter column 目标列 datetime
我试了下,你直接修改就可以了.
----------------------------------
create table t (
rq varchar(20))

insert t
select '20050701 '

select * from t

alter table t alter column rq datetime

select * from t
exec sp_columns t
drop table t
结果
20050701
2005-07-01 00:00:00.000
Northwind dbo t rq 11 datetime 23 16 3 NULL 1 NULL NULL 9 3 NULL 1 YES 111