日期:2014-05-17  浏览次数:20738 次

把字符转成日期插入到另一字段?
同一张表,原来有一字段accessdate存放日期的,但类型是varchar2,现在新建一字段accessdate1,类型是date。现在的想法是通过存储过程将accessdate转换成日期插入到accessdate1中去,过程如下:
declare
  cursor c1 is select count(id) from taccessinfo;
  cursor c2 is select to_date(accessdate,'yyyy-mm-dd') from taccessinfo;
  v_num number;
begin
  for r1 in c1 loop
for r2 in c2 loop
  exit when c1%notfound or c2%notfound;
  insert into taccessinfo(accessdate1) values(r2);
  v_num:=v_num+1;
  if v_num=1000 then
  commit;
  v_num:=0;
  end if;
  end loop;
  end loop;
commit;
end;

这个无法实现此功能,求解?

------解决方案--------------------
update yourTableName set accessdate1=to_date(accessdate);
或者
update yourTableName set accessdate1=to_date(accessdate,'yyyy-mm-dd hh24:mi:ss');

------解决方案--------------------
如楼上,不需要存储过程哇,一条语句就好,主要看你原来字段存放的格式是什么
update taccessinfo set accessdate1=to_date(accessdate,'yyyy-mm-dd');
------解决方案--------------------
直接update 进去
------解决方案--------------------
cursor c2 is select to_date(accessdate,'yyyy-mm-dd') as acc_date from taccessinfo;

insert into taccessinfo(accessdate1) values(r2);

r2是个cursor,可以这样用的吗?应该是r2.acc_date吧
------解决方案--------------------
update taccessinfo set accessdate1=to_date(accessdate,'yyyy-mm-dd hh24:mi:ss');
------解决方案--------------------
直接update的时候进行类型转换就好了,使用to_date函数就OK了,不需要单独写个存储过程进行转换