How to add leading zero in a number in Oracle SQL query?

Madhusudan

I am retrieving a column named removal_count in my query using COUNT() function. In result set the datatype of removal_count is BIGDECIMAL. I want to convert number into five digits. SO if value is less than five digits then it should be represented with leading zero's.

e.g 1) If removal count is 540 then display 00540
2) If removal count is 60 then display 00060

If the removal count is integer/string value then I can add leading zero's using java expression :

--if removal_count is integer--
String.format("%05d",removal_count)

--if removal_count is string--
("00000"+removal_count).subString(removal_count.length())

Can we convert removal_count into string or integer ( from big decimal) so that I can use given java expression? Or else is there any way to add leading zero's in query itself?

Lalit Kumar B

You could do it in two ways.

Method 1

Using LPAD.

For example,

SQL> WITH DATA(num) AS(
  2  SELECT 540 FROM dual UNION ALL
  3  SELECT 60 FROM dual UNION ALL
  4  SELECT 2 FROM dual
  5  )
  6  SELECT num, lpad(num, 5, '0') num_pad FROM DATA;

       NUM NUM_P
---------- -----
       540 00540
        60 00060
         2 00002

SQL>

The WITH clause is only to build sample data for demo, in your actual query just do:

lpad(removal_count, 5, '0')

Remember, a number cannot have leading zeroes. The output of above query is a string and not a number.

Method 2

Using TO_CHAR and format model:

SQL> WITH DATA(num) AS(
  2  SELECT 540 FROM dual UNION ALL
  3  SELECT 60 FROM dual UNION ALL
  4  SELECT 2 FROM dual
  5  )
  6  SELECT num, to_char(num, '00000') num_pad FROM DATA;

       NUM NUM_PA
---------- ------
       540  00540
        60  00060
         2  00002

SQL>

Update : To avoid the extra leading space which is used for minus sign, use FM in the TO_CHAR format:

Without FM:

SELECT TO_CHAR(1, '00000') num_pad,
  LENGTH(TO_CHAR(1, '00000')) tot_len
FROM dual;

NUM_PAD    TOT_LEN
------- ----------
 00001           6 

With FM:

SELECT TO_CHAR(1, 'FM00000') num_pad,
  LENGTH(TO_CHAR(1, 'FM00000')) tot_len
FROM dual;

NUM_PAD    TOT_LEN
------- ----------
00001            5

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Oracle, how to match leading zero number in varchar?

From Dev

How to add leading zero when number is less than 10?

From Dev

how to give a number a leading zero in scheme

From Dev

How to add leading zero formatting to string in Pandas?

From Dev

How to add Leading Zero with getMonth in Java (Android)

From Dev

How to add leading zero formatting to string in Pandas?

From Dev

FullCalendar - how to add leading zero to getUTCMinutes function

From Dev

Create leading zero in Oracle

From Dev

Add leading Zero Python

From Dev

SQL Query to Add Leading 0 to part of a column

From Dev

T-SQL Insert number with leading zero into table

From Dev

How to add serial number to a range query in SQL?

From Dev

Add a sequence number for each element in a group using an Oracle SQL query

From Dev

How to add the weeks with no items sold as zero values to my SQL query?

From Dev

How to add a number with leading zeroes in php?

From Dev

How to Use corrplot with method="number" and Drop Leading Zero?

From Dev

How to Use corrplot with method="number" and Drop Leading Zero?

From Dev

Force leading zero in number input

From Dev

PHP: How to add leading zeros/zero padding to float via sprintf()?

From Dev

How to add leading padded zeros until 4 characters after that original number should come in SQL Server 2008?

From Dev

SQL query add zero if the value is a single digit

From Dev

How to pad leading zero in javascript

From Dev

How to trim leading zero in Hive

From Dev

How to format to display leading zero

From Dev

Displaying a number in excel without the leading zero

From Dev

Add leading zeroes to number in Dart

From Java

Add leading zeroes to number in Java?

From Dev

Add leading zero to hex values in python

From Dev

Regex to add leading zero in date record

Related Related

  1. 1

    Oracle, how to match leading zero number in varchar?

  2. 2

    How to add leading zero when number is less than 10?

  3. 3

    how to give a number a leading zero in scheme

  4. 4

    How to add leading zero formatting to string in Pandas?

  5. 5

    How to add Leading Zero with getMonth in Java (Android)

  6. 6

    How to add leading zero formatting to string in Pandas?

  7. 7

    FullCalendar - how to add leading zero to getUTCMinutes function

  8. 8

    Create leading zero in Oracle

  9. 9

    Add leading Zero Python

  10. 10

    SQL Query to Add Leading 0 to part of a column

  11. 11

    T-SQL Insert number with leading zero into table

  12. 12

    How to add serial number to a range query in SQL?

  13. 13

    Add a sequence number for each element in a group using an Oracle SQL query

  14. 14

    How to add the weeks with no items sold as zero values to my SQL query?

  15. 15

    How to add a number with leading zeroes in php?

  16. 16

    How to Use corrplot with method="number" and Drop Leading Zero?

  17. 17

    How to Use corrplot with method="number" and Drop Leading Zero?

  18. 18

    Force leading zero in number input

  19. 19

    PHP: How to add leading zeros/zero padding to float via sprintf()?

  20. 20

    How to add leading padded zeros until 4 characters after that original number should come in SQL Server 2008?

  21. 21

    SQL query add zero if the value is a single digit

  22. 22

    How to pad leading zero in javascript

  23. 23

    How to trim leading zero in Hive

  24. 24

    How to format to display leading zero

  25. 25

    Displaying a number in excel without the leading zero

  26. 26

    Add leading zeroes to number in Dart

  27. 27

    Add leading zeroes to number in Java?

  28. 28

    Add leading zero to hex values in python

  29. 29

    Regex to add leading zero in date record

HotTag

Archive