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 .
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.
Best Answer In any case, to show a NUMBER with leading zeros: select TO_CHAR(3, 'FM000') from dual; will show the string '003'.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With