早教吧作业答案频道 -->其他-->
为sql一字段值中每个特定字符前的最后一个数字加1怎么处理?如:sql中一个字段值为:1788987565327、768374872394903、21437238740213483874629、23412341234252345。其中顿号间隔的每一组数字位数和尾数不
题目详情
为sql一字段值中每个特定字符前的最后一个数字加1怎么处理?
如:sql中一个字段值为:1788987565327、768374872394903、21437238740213483874629、23412341234252345。其中顿号间隔的每一组数字位数和尾数不定,现在要使前面这个字段值中顿号前的数字尾数即7、3、9、5都分别加1,变成8、4、0、6输出成1788987565328、768374872394904、21437238740213483874620、23412341234252346。注意其中第三个数,从9加1后,输出成0,而不是10。多谢啦。
如:sql中一个字段值为:1788987565327、768374872394903、21437238740213483874629、23412341234252345。其中顿号间隔的每一组数字位数和尾数不定,现在要使前面这个字段值中顿号前的数字尾数即7、3、9、5都分别加1,变成8、4、0、6输出成1788987565328、768374872394904、21437238740213483874620、23412341234252346。注意其中第三个数,从9加1后,输出成0,而不是10。多谢啦。
▼优质解答
答案和解析
你这必须得采纳我的了,中午都没休息,帮你写了,你运行看看结果:
declare
targetstr varchar2(2000);
strlength number;
position number;
maxposition number;
retrunstr varchar2(2000);
tempstr varchar2(2000);
endstr number;
begin
targetstr := '1788987565327、768374872394903、21437238740213483874629、23412341234252345';
maxposition := 0;
select LENGTH(targetstr) into strlength from dual;
for i in 1..strlength loop
select instr(str,'、',1,i) into position from (select targetstr as str from dual);
--dbms_output.PUT_LINE(position);
if position > 0 then
if maxposition = 0 then
select substr(str,0,instr(str,'、',1,1)-1) into retrunstr from (select targetstr as str from dual);
select TO_NUMBER(substr(restr,-1)) into endstr from (select retrunstr as restr from dual);
if endstr = 3 or endstr = 5 or endstr = 7 then
endstr := endstr + 1;
elsif endstr = 9 then
endstr := 0;
end if;
select substr(str,0,instr(str,'、',1,1)-2)||TO_CHAR(endstr) into retrunstr from (select targetstr as str from dual);
elsif maxposition < position then
select substr(str,instr(str,'、',1,i-1)+1,instr(str,'、',1,i)-instr(str,'、',1,i-1)-1) into tempstr from (select targetstr as str from dual);
select TO_NUMBER(substr(restr,-1)) into endstr from (select tempstr as restr from dual);
if endstr = 3 or endstr = 5 or endstr = 7 then
endstr := endstr + 1;
elsif endstr = 9 then
endstr := 0;
end if;
select substr(str,1,length(str)-1) ||TO_CHAR(endstr) into tempstr from (select tempstr as str from dual);
retrunstr := retrunstr || '、'|| tempstr;
end if;
maxposition := position;
else
if maxposition > position then
--特别处理最后一段
tempstr := '';
select substr(str,maxposition-length(str)) into tempstr from (select targetstr as str from dual);
select TO_NUMBER(substr(restr,-1)) into endstr from (select tempstr as restr from dual);
if endstr = 3 or endstr = 5 or endstr = 7 then
endstr := endstr + 1;
elsif endstr = 9 then
endstr := 0;
end if;
select substr(str,1,length(str)-1) ||TO_CHAR(endstr) into tempstr from (select tempstr as str from dual);
retrunstr := retrunstr || '、'|| tempstr;
end if;
exit;
end if;
end loop;
dbms_output.PUT_LINE(retrunstr);
end;
运行结果如下:
14:03:38 **** SCRIPT STARTED: 02-Apr-2015 14:03:38 ****
14:03:38 declare
14:03:38 targetstr varchar2(2000);
14:03:38 ...
14:03:39 PL/SQL block executed
1788987565328、768374872394904、21437238740213483874620、23412341234252346
14:03:39 **** SCRIPT ENDED 02-Apr-2015 14:03:39 ****
14:03:39 End Script Execution
declare
targetstr varchar2(2000);
strlength number;
position number;
maxposition number;
retrunstr varchar2(2000);
tempstr varchar2(2000);
endstr number;
begin
targetstr := '1788987565327、768374872394903、21437238740213483874629、23412341234252345';
maxposition := 0;
select LENGTH(targetstr) into strlength from dual;
for i in 1..strlength loop
select instr(str,'、',1,i) into position from (select targetstr as str from dual);
--dbms_output.PUT_LINE(position);
if position > 0 then
if maxposition = 0 then
select substr(str,0,instr(str,'、',1,1)-1) into retrunstr from (select targetstr as str from dual);
select TO_NUMBER(substr(restr,-1)) into endstr from (select retrunstr as restr from dual);
if endstr = 3 or endstr = 5 or endstr = 7 then
endstr := endstr + 1;
elsif endstr = 9 then
endstr := 0;
end if;
select substr(str,0,instr(str,'、',1,1)-2)||TO_CHAR(endstr) into retrunstr from (select targetstr as str from dual);
elsif maxposition < position then
select substr(str,instr(str,'、',1,i-1)+1,instr(str,'、',1,i)-instr(str,'、',1,i-1)-1) into tempstr from (select targetstr as str from dual);
select TO_NUMBER(substr(restr,-1)) into endstr from (select tempstr as restr from dual);
if endstr = 3 or endstr = 5 or endstr = 7 then
endstr := endstr + 1;
elsif endstr = 9 then
endstr := 0;
end if;
select substr(str,1,length(str)-1) ||TO_CHAR(endstr) into tempstr from (select tempstr as str from dual);
retrunstr := retrunstr || '、'|| tempstr;
end if;
maxposition := position;
else
if maxposition > position then
--特别处理最后一段
tempstr := '';
select substr(str,maxposition-length(str)) into tempstr from (select targetstr as str from dual);
select TO_NUMBER(substr(restr,-1)) into endstr from (select tempstr as restr from dual);
if endstr = 3 or endstr = 5 or endstr = 7 then
endstr := endstr + 1;
elsif endstr = 9 then
endstr := 0;
end if;
select substr(str,1,length(str)-1) ||TO_CHAR(endstr) into tempstr from (select tempstr as str from dual);
retrunstr := retrunstr || '、'|| tempstr;
end if;
exit;
end if;
end loop;
dbms_output.PUT_LINE(retrunstr);
end;
运行结果如下:
14:03:38 **** SCRIPT STARTED: 02-Apr-2015 14:03:38 ****
14:03:38 declare
14:03:38 targetstr varchar2(2000);
14:03:38 ...
14:03:39 PL/SQL block executed
1788987565328、768374872394904、21437238740213483874620、23412341234252346
14:03:39 **** SCRIPT ENDED 02-Apr-2015 14:03:39 ****
14:03:39 End Script Execution
看了 为sql一字段值中每个特定字...的网友还看了以下:
请教数字的中文读法请问数字11111怎么读?一万一千一百一十一还是一万零一千一百一十一?刚刚在某处 2020-04-07 …
新配了眼镜,看远处很清晰,看近处头晕?前几天新配了一副眼镜,两眼度数差距很大,左眼度数425,右眼 2020-05-16 …
什么是处数处数怎么算 2020-06-12 …
公元前的年数怎样顺序计算的?夏朝起讫年代约公元前2146-1675,为什么这个朝代的始在与2146 2020-06-16 …
在甲处劳动的有26人,在乙处劳动的有19人,另有9人在树荫下休息,在不改变三处总人数的前提下,1. 2020-07-15 …
对数前的倍数怎么处理如log5^35-2log5^三分之七 2020-07-22 …
怎么分奇偶性求数列的通项公式?an=n,n为奇数,an=2×3∧(n/2-1),n为偶数.我不知道 2020-08-01 …
我土地上前有不知何处人士放置无主神像,我该如何处理安置较妥?我土地只有2平而已,数年前有不知名人士以 2020-11-27 …
英语冠词问题Jimwasthefirsttobreak----silence.---处该怎么填?是填 2020-12-17 …
一道数学题一个等差数列Tn=(a1+a2+...+a34)-(a35+a36+...+an)是怎么变 2021-01-14 …