Home >Database >Mysql Tutorial >Oracle字符串中数字处理

Oracle字符串中数字处理

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:01:571444browse

输出结果为123456789A0001357 ;发现substr(v_i,14)截取后七位进行计算时,pl/sql会自动把字符转换成整数,把前面的lsquo;0rsqu

最近开始写存储过程,有一个需求,数据库表中有个字段(20位序列号,里面可能有字符,后7位都是数字),,要根据数量对该字段进行加计算

输出结果为123456789A0001357 ; 

发现substr(v_i,14)截取后七位进行计算时,pl/sql会自动把字符转换成整数,把前面的‘0’去掉了。

研究Oracle内部函数,发现有个函数可以进行格式转换成字符串 to_char(exp,'reg') exp为要转换的数字等,reg为转换格式

修改v_i := substr(v_i,1,13)||to_char(substr(v_i,14)+i,'0000000');--reg为7个‘0’,如果要保持位数的正确,前面补‘0’

输出结果为:123456789A000 0001357,

发现中间多个空格,不知道什么原因。继续使用oracle 内部函数trim(exp)去掉exp两端空格

修改为:v_i := substr(v_i,1,13)||trim(to_char(substr(v_i,14)+i,'0000000')); 

输出结果为:123456789A0000001357,。这是我们想要的

总结,要对oracle内部函数很熟悉啊,不过上面可能有个BUG,如果后七位产生进位,上面处理就不正确了,所有要通过业务来控制字符截取位数。

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:MySQL修复myisam表Next article:Linux下MySQL主从配置