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!

Apply leading zeros

MuzzSep 30 2015 — edited Oct 1 2015

Oracle 10 g

Hi Gurus

I appreciate if someone help me out and suggest me some better option to do this task...

I wrote the following code in which I have to format by using leading zeros, I wrote the following query for that purpose but I'm wondering that if there is some short query to do the same task, please see below my code..

WITH datum AS

(

SELECT 1 num_format FROM dual

UNION ALL

SELECT 10 FROM dual

UNION ALL

SELECT 100 FROM dual

UNION ALL

SELECT 1000 FROM dual

)

SELECT num_format,CASE WHEN LENGTH(num_format)=1 THEN '000'||num_format

                       WHEN LENGTH(num_format)=2 THEN '00'||num_format

                       WHEN LENGTH(num_format)=3 THEN '0'||num_format

                       WHEN LENGTH(num_format)=4 THEN to_char(num_format)

END MODI_FORMAT

FROM datum;

Result

NUM_FORMAT, MODI_FORMAT

1                0001

10              0010

100            0100

1000         1000

Regards

Shumail

This post has been answered by Frank Kulash on Sep 30 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 28 2015
Added on Sep 30 2015
10 comments
3,099 views