Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle, adding leading zeros to string (not number)

I am using Oracle (work space is TOAD) and I need to make my strings that if they are shorted then 10 characters then add leading zeros to make them all 10 digit strings.

For example if I have a string like this: '12H89' need to be '0000012H89' or '1234' to be '0000001234'

How can this be done? Whats the best way?

Thanks in advance .

like image 285
Ovi Avatar asked May 22 '14 18:05

Ovi


People also ask

How do you add leading zeros to a string?

You can add leading zeros to an integer by using the "D" standard numeric format string with a precision specifier. You can add leading zeros to both integer and floating-point numbers by using a custom numeric format string.

How do you add leading zeros in Oracle?

Best Answer In any case, to show a NUMBER with leading zeros: select TO_CHAR(3, 'FM000') from dual; will show the string '003'.

How do you add a zero in front of a string in Java?

The format() method of String class in Java 5 is the first choice. You just need to add "%03d" to add 3 leading zeros in an Integer. Formatting instruction to String starts with "%" and 0 is the character which is used in padding.

What is Lpad and RPAD in Oracle?

LPAD is used to pad the left side of a base string with a given pad string. It will repeat the pad string until the given length is met. RPAD is similar but adds the padding on the right side of the base string.


1 Answers

You can use the LPAD function for that, passing in the string, the length you want it to be, and the character to pad it with. For 10 digits with leading zeroes this would be:

LPAD('12H89', 10, '0') 

The return value is the padded string.

See: http://www.techonthenet.com/oracle/functions/lpad.php

like image 195
GolezTrol Avatar answered Oct 13 '22 05:10

GolezTrol