cihadakt
cihadakt

Reputation: 3214

how to put specific number of characters in front of a string in pl/sql?

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 0s in front. Or if my string is 12345678 then new string should be 00000000000012345678 (20 chars) with 12 0s 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

Answers (3)

David Faber
David Faber

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

Hauke
Hauke

Reputation: 242

do it like this:

select lpad('1455',20,'0') from dual;

Upvotes: 2

Lukasz Szozda
Lukasz Szozda

Reputation: 175726

You could use TO_CHAR:

SELECT TO_CHAR('1455', 'fm00000000000000000000') FROM dual;

DBFiddle Demo

Upvotes: 0

Related Questions