Connor and Chris will both be at AI World from October 12 to October 17 , the premier Oracle conference of 2025. If you're in Vegas, please come say Hi or pop into our sessions
Thanks for the question, Shyam.
Asked: February 12, 2020 - 2:51 pm UTC
Last updated: February 14, 2020 - 12:35 pm UTC
Version: 4.2
Viewed 1000+ times
SQL> with t as 2 ( select 'MFT123456' x from dual union all 3 select 'LCC1234567' x from dual ) 4 select 5 substr(x,1,7-length(x)+regexp_instr(x,'\d'))|| regexp_substr(x,'\d+') str 6 from t; STR -------------------------------------------------------------------------------- MF123456 L1234567
Mikhail Velikikh, February 14, 2020 - 2:28 pm UTC
SQL> with t(x) as ( 2 select 'MFT123456' from dual union all 3 select 'LCC1234567' from dual union all 4 select 'A123456' from dual union all 5 select 'B52' from dual) 6 select x, 7 substr(x,1,7-length(x)+regexp_instr(x,'\d'))||regexp_substr(x,'\d+') str, 8 regexp_replace(x, '^(\D*)(\D{'||greatest(length(x)-8,0)||'})(\d+)$', '\1\3') str2 9 from t; X STR STR2 ---------- ------------ ------------ MFT123456 MF123456 MF123456 LCC1234567 L1234567 L1234567 A123456 A1123456 A123456 B52 B5252 B52
Check out more PL/SQL tutorials on our LiveSQL tool.
PL/SQL reference manual from the Oracle documentation library