SQL> select to_char(123,'000000') from dual;TO_CHAR ------- 000123
update from table_name set col=to_char(col,'000000') where ......
update mytable set col = lpad(col,6,'0') where length(col) < 7 and ??? 请教了
to_char(col,'000000')会报错,因为有非数字型的记录。
col varchar2(16) 可能有任何字符,不过一般是中文字符。也可能is null
update circuit_bak a set circuit_name=lpad('llll',10,'0') where length(circuit_name)=lengthb(circuit_name) and exists(select circuit_name from (select transLate(circuit_name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','00000000000000000000000000') circuit_nm from circuit_bak) b where b.circuit_nm=a.circuit_name) 可以实现中文加字符(小写的、特殊字符可以添加到translate的字符序列中)试一下,这个方法比较麻烦,看其他人有没有更好的方法
呵呵,没看懂! 对了,可不可以这样: update mytable set col = to_char(col, '000000') where col >= '0' and col <= '99999';
少了点 update circuit_bak a set circuit_name=lpad('llll',10,'0') where length(circuit_name)=lengthb(circuit_name) and lengthb(circuit_name)<7 and exists(select circuit_name from (select transLate(circuit_name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','00000000000000000000000000') circuit_nm from circuit_bak) b where b.circuit_nm=a.circuit_name)
谢谢!但lpad好象不管字段中内容是不是为数字型都会左填充,而且会截断多出的字符。
这些你可以在选择更新的条件中进行一下限制的
-------
000123
set col=to_char(col,'000000')
where ......
请教了
可能有任何字符,不过一般是中文字符。也可能is null
where length(circuit_name)=lengthb(circuit_name) and exists(select circuit_name from (select transLate(circuit_name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','00000000000000000000000000') circuit_nm from circuit_bak) b where b.circuit_nm=a.circuit_name)
可以实现中文加字符(小写的、特殊字符可以添加到translate的字符序列中)试一下,这个方法比较麻烦,看其他人有没有更好的方法
对了,可不可以这样:
update mytable set col = to_char(col, '000000') where col >= '0' and col <= '99999';
update circuit_bak a set circuit_name=lpad('llll',10,'0')
where length(circuit_name)=lengthb(circuit_name) and lengthb(circuit_name)<7 and exists(select circuit_name from (select transLate(circuit_name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','00000000000000000000000000') circuit_nm from circuit_bak) b where b.circuit_nm=a.circuit_name)
translate实现按字符一对一的翻译,将原来的字符串先转换一下,看其中是否有改动过的字符,有的话就不用更新了,没有的-在其他前提条件下(长度小于7,没有中文)进行更新!