我有这个字符串
ABCD-0490-ABCD
我使用substr('abcd-0490-abcd',5,4)
获取数字部分,但问题是我想将该数字增加1,所以我添加to_number就像那样
to_number(substr('abcd-0490-abcd', 5,4 ))
这将删除左侧的零号码,这对我的脚本来说是错误的,所以它没有抓住我想要的正确数据
无论如何要避免这个
create or replace procedure pro
(yy in varchar2 default '[0-9]{2}',mm in varchar2 default '[0-9]{2}') as pattern varchar2(80);
cursor cur (pattern varchar2) is
with t as
(
select
substr(column1, 5,4 ) as seq,
substr(column1, 10, 2) as yy,
substr(column1, 13, 2) as mm,
substr(column1, 16, 2) as dd
from test1
where regexp_like(column1, pattern)
),
r (yy, mm, dd, seq, max_seq) as (
select yy, mm, dd, min(seq), max(seq)
from t
group by yy, mm, dd
union all
select yy, mm, dd, seq + 1, max_seq
from r
where seq + 1 <= max_seq
)
select yy, mm, dd, seq as missing_seq
from r
where not exists (
select 1 from t
where t.yy = r.yy
and t.mm = r.mm
and t.dd = r.dd
and t.seq = r.seq
)
order by yy, mm, dd, seq;
begin
pattern := 'Cabcd[-][0-9]{4}[_][0-9]{2}'|| yy ||'[_][0-9]{2}' || mm || '[_][0-9]{2}[_][0-9]{4}[_][T]["2"]';
for rec in cur(pattern) loop
dbms_output.put_line(rec.missing_seq);
end loop;
dbms_output.put_line('Done');
end pro;
/
答案 0 :(得分:4)
使用LPAD功能:http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions095.htm#SQLRF00663
select lpad( to_number(substr('abcd-0490-abcd', 6,4 )) + 1 , 4, '0')
from dual;
答案 1 :(得分:1)
默认的数字到字符转换不会产生前导零。
明确TO_CHAR
来定义您的格式:
SELECT TO_CHAR(490, 'FM0000') FROM dual;
0490
修改:这是您的查询示例。
SELECT TO_CHAR( TO_NUMBER( SUBSTR( 'abcd-0490-abcd', 6, 4 ) ) + 1, 'FM0000' )
FROM dual;
0491
答案 2 :(得分:0)
最好你尝试“rownum”进行增量
SELECT TO_CHAR(TO_NUMBER(SUBSTR('abcd-0490-abcd',6,4))+ rownum,'FM0000') FROM dual;
在ur数据库中使用table dual而不是任何其他表。
SELECT TO_CHAR(TO_NUMBER(SUBSTR('abcd-0490-abcd',6,4))+ rownum,'FM0000') 来自'your_tablename';