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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

TO_CHAR - NUMBER(5) - leading and trailing zeroes

Brian TkatchJan 7 2014 — edited Jan 8 2014

I am reviewing some code and came across: RPAD(LPAD(TO_CHAR(col), 9, '0'), 11, '0') where col is NUMBER(5, 0) NOT NULL DEFAULT 0.

FWIW (in QA): SELECT LENGTH(col) L, SIGN(col) S, COUNT(*) C FROM tab GROUP BY LENGTH(col), SIGN(col) ORDER BY 1,2;

1    0    22196

1    1    5746

2    1    53553

3    1    410260

4    -1    16

4    1    23198

5    1    25

The sample output i have contains no negative numbers. Seemingly, it is excluded by the query with other logic (though not directly) or this data is only in test cases.

The intention seems to be to add up to 8 leading zeroes and 2 trailing zeroes. One of the pad functions seems redundant as TO_CHAR itself can do the work, although it does not seem that TO_CHAR can add both leading and trailing zeroes in the same call. Since the latter is only two zeroes, i figure simply concatenating them is better: TO_CHAR(col, 'FM000000009') || '00'. Technically, this is not the same as the current expression, as where the number is negative, the sign is placed in the middle of the string! I assume this not intended as the case is not expected. Perhaps i can "fix" it and leave a comment explaining the original behavior.

Is there a way to add both preceding and trailing zeroes with one function?

Message was edited by: Brian Tkatch Put the parenthesis in the correct place. Oops!

This post has been answered by Frank Kulash on Jan 7 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 5 2014
Added on Jan 7 2014
8 comments
1,828 views