Reputation: 3214
I want to fix a string to length of 20 characters after the operation. For example, if my string is 1455
then new string should be 00000000000000001455
(20 characters) with 16 0
s in front. Or if my string is 12345678
then new string should be 00000000000012345678
(20 chars) with 12 0
s in front.
I can do it in a user-defined function but I wonder if there is an easy way to do it in Oracle? I googled but couldn't find any similar solution. Any suggestions?
Upvotes: 1
Views: 2455
Reputation: 12485
You can use LPAD()
:
SELECT LPAD('1455', 20, '0') FROM dual;
or generally:
SELECT LPAD(mystring, 20, '0') FROM mytable;
If the length of mystring
is greater than 20 characters it will be truncated to 20 characters (and not padded!).
Hope this helps.
Upvotes: 1
Reputation: 175726
You could use TO_CHAR
:
SELECT TO_CHAR('1455', 'fm00000000000000000000') FROM dual;
Upvotes: 0