I have a column, which stores a 4 character long string with 4 or less wild characters (for eg. ????, ??01‘, 0??1 etc). For each such string like 0??1 I have to insert into another table values 0001 to 0991; for the string ??01, values will be be 0001 to 9901; for string ???? values will be 0000 to 9999 and so on.
How could I accomplish this using PL/SQL and string functions?
EDIT
The current code is:
declare
v_rule varchar2(50) := '????52132';
v_cc varchar2(50);
v_nat varchar2(50);
v_wild number;
n number;
begin
v_cc := substr(v_rule,1,4);
v_nat := substr(v_rule,5);
dbms_output.put_line (v_cc || ' '|| v_nat);
if instr(v_cc, '????') <> 0 then
v_wild := 4;
end if;
n := power(10,v_wild);
for i in 0 .. n - 1 loop
dbms_output.put_line(substr(lpad(to_char(i),v_wild,'0' ),0,4));
end loop;
end;
/
Would something like the following help?
This assumes you have a table called
some_tablewith a columnrule, which contains text such as??01,0??1and????. It inserts intosome_other_tableall numbers from 0000 to 9999 that match these wild-carded patterns.The subquery
generates all numbers in the range
0000to9999. We then filter out from this list of numbers any that match this pattern, usingLIKE. Note that_is the single-character wildcard when usingLIKE, not?.I set this up with the following data:
After running the above PL/SQL block, the table
some_other_tablehad 10200 rows in it, all the numbers that matched all three of the patterns given.