Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Padding numbers within a string

Matthew MorganJan 10 2013 — edited Jan 11 2013
Hi Folks,

I have some strings which are all a mix of letters and numbers - no particular rules, but no special characters.
For ordering purposes, I would like to be able to lpad each number string to 5 digits (in reality there will never be more than 3 digits in a row)

For example:

XX01 -> XX00001
XX100 -> XX00100
XX1YY10ZZ1 ->XX00001YY00010ZZ00001

Any thoughts on how this can be achieved?

I am currently trying to get something working with REGEXP_REPLACE but cant quite get it working:

select pc.pcode,REGEXP_REPLACE(pc.pcode, '([[:digit:]]+)',lpad('\1',5,0))
from test_tab pc
order by 2
/

The actual/desired results are as follows:

ORIG DESIRED RESULT ACTUAL RESULT
=== =========== ==========
XX01 XX00001 XX00001 (correct)
XX10 XX00010 XX00010 (correct)
XX100 XX00100 XX000100 (incorrect)
XX1YY10ZZ1 XX00001YY00010ZZ00001 XX0001YY00010ZZ0001 (incorrect)

Thanks for any input you may have!

Matt

Edited by: Matthew Morgan on Jan 10, 2013 7:59 PM

Edited by: Matthew Morgan on Jan 10, 2013 7:59 PM

Edited by: Matthew Morgan on Jan 10, 2013 8:00 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 8 2013
Added on Jan 10 2013
4 comments
466 views