Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DB2 SQL Convert Decimal to Character with Padded Zeros

Tags:

sql

db2

How can I convert my DECIMAL(11) field from 12345678 to a character value of 00012345678?

like image 879
macunte Avatar asked Mar 20 '13 21:03

macunte


People also ask

What is Lpad in db2?

The LPAD function pads a string on the left with a specified character string or with blanks. LPAD ( string-expression , integer , pad ) The schema is SYSIBM. The LPAD function treats leading or trailing blanks in the string expression as significant.

How do you convert a decimal to a whole number in SQL?

Using ROUND() function: This function in SQL Server is used to round off a specified number to a specified decimal places. Using FLOOR() function: It returns the largest integer value that is less than or equal to a number.

How do you remove leading zeros in db2?

5) you can use the SQL standard TRIM() function: db2 => CREATE TABLE tbl (vc VARCHAR(64)) DB20000I The SQL command completed successfully. db2 => INSERT INTO tbl (vc) VALUES ('00001 00'), ('00026 00') DB20000I The SQL command completed successfully.


2 Answers

Only use the DIGITS function, because this verifies the length of the field numeric or decimal, etc and completes with zeros to the left when is necessary.

SELECT DIGITS(FIELD) FROM ...

The length of the resulting string is always:

  • 5 if the argument is a small integer
  • 10 if the argument is a large integer
  • 19 if the argument is a big integer
like image 119
kelgwiin Avatar answered Sep 20 '22 14:09

kelgwiin


Based on your comment in @Mr Fuzzy Botton's answer, I'm guessing you're on DB2 for i, which does not have the LPAD function. You could instead use a combination of the REPEAT and RIGHTfunctions:

SELECT RIGHT(REPEAT('0', 11) || LTRIM(CHAR(your_field)), 11)
FROM your_table
like image 38
bhamby Avatar answered Sep 18 '22 14:09

bhamby