字段从varchar2修改为number,字段中的内容做修改,替换

#测试表的内容结构:如下所示:

其中DATEHH字段:代表时间,字段在表中是varchar2格式

现有如下需求:字段类型,从varchar2改变为number,

                      字段中 ‘。’去除,

                 2013103少一个0,需要添加,月份可能会少0,

              42开头的历史数据不动

SQL> select * from c;

        ID NAME       DATEHH

---------- ---------- ----------         

1 yang       2016.0323         

2 cheng      2013.3213        

23 aefe       2013.1103         

4 qewrqwer   2013.103         

5 sasrqwooo  2013.1201         

6 swwwww     2013.201         

7 qqw        4289         

8 eew e      4223

8 rows selected.

#函数replace替换.  => null

SQL>  update c set datehh=replace(datehh,'.',null);

#函数截取+连接符,where条件筛选需要修改的问题value

SQL> update c set datehh=(substr(datehh,1,4))||'0'||(substr(datehh,5)) where length(datehh)<8 and datehh not like '42%';

#为表增加一个新的字段
SQL> alter table c add new_a number(10);
#更新字段
SQL>  update c set new_a=datehh;
SQL> commit;
#删除字段
SQL> alter table c drop column datehh;
#改名
SQL> alter table c rename column new_a to datehh;
原文地址:https://www.cnblogs.com/lvcha001/p/8834849.html