0

If the POSITION_ID is not 6 digit I want the preceding spaces to be 0

For example-

1102 should be 001102, 304 should be 000302

For this i used the below query -

  select LPAD(REGEXP_REPLACE(position_id,'\D+'),6,'0')  test from dual

But this query is adding 2 in front of numbers that are already 6.

For example 110092 is coming as 211009 345679 is coming as 234567 106698 is coming as 210669

How to correct the query I have used above

SSA_Tech124
  • 577
  • 1
  • 9
  • 25
  • Maybe [built-in] function [TO_CHAR](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/TO_CHAR-number.html#GUID-00DA076D-2468-41AB-A3AC-CC78DBA0D9CB) is appropriate? By the way, did you make a mistake in the example in your question? Why should **304** become **000302** ? – Abra Jun 14 '23 at 04:01

2 Answers2

2

The function to get a formatted string from a number in Oracle is TO_CHAR. The format to get a six-digit integer is 'FM000000'.

SELECT TO_CHAR(position_id, 'FM000000') FROM mytable;

Docs: TO_CHAR, Number format models, FM

Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73
0

simple lpad should work. Use also ltrim to remove spaces.

LPAD(ltrim(rtrim(position_id)),6,'0') 
Koushik Roy
  • 6,868
  • 2
  • 12
  • 33