sql - How to select sub string in oracle? -
i have scenario data below:
chapter 18 unit 10 sect 16 case 1 : want select chapter 18 above string. case 2 : want select unit 10 above string. case 3 : want select sect 16 above string.
thanks & regards, keerthi
using substr:
declare l_start number := dbms_utility.get_cpu_time; begin in ( t ( select 'chapter ' || level || ' unit ' || level || ' sect ' || level d dual connect rownum < 100000 ) select substr(d, 1, instr(d, ' ', 1, 2) - 1) chapter , substr(d, instr(d, ' ', 1, 2), instr(d, ' ', 1, 4) - instr(d, ' ', 1, 2) ) unit , substr(d, instr(d, ' ', 1, 4), length(d) - instr(d, ' ', 1, 4) + 1 ) sect t ) loop null; end loop; dbms_output.put_line((dbms_utility.get_cpu_time - l_start) || ' hsec'); end; 126 hsec
using regexp:
declare l_start number := dbms_utility.get_cpu_time; begin in ( t ( select 'chapter ' || level || ' unit ' || level || ' sect ' || level d dual connect rownum < 100000 ) select regexp_substr(d, 'chapter [0-9]*') chapter , regexp_substr(d, 'unit [0-9]*') unit , regexp_substr(d, 'sect [0-9]*') sect t ) loop null; end loop; dbms_output.put_line((dbms_utility.get_cpu_time - l_start) || ' hsec'); end; 190 hsec
so solution regexp slower, more readable, if use regexp.
Comments
Post a Comment