8

I am trying to concatenate strings in oracle.

The following is my query:

insert into dummy values('c'||to_char(10000,'99999'));

The expected result is:

c10000

But the output I get is with a space in between 'c' and the value 10000:

c 10000 

How to concat without spaces?

2 Answers 2

21

This is not an issue with the concatenation operator but with the function to_char(). Try instead:

to_char(10000,'FM99999')

I quote the manual here:

FM .. Returns a value with no leading or trailing blanks.

Sign up to request clarification or add additional context in comments.

Comments

-1

There are two solutions:

  1. Fill Mode ('FM') formatting prefix that suppresses the additional blank character prefix for the to_char number conversion. I suggest this one is preferred, because it is integrated with the to_char format and does not require an additional function call;
  2. LTRIM of the returned value from the to_char number conversion.

The code below shows the results of both solutions:

Select concat('NTA', to_char(1,'FM0000000000000')), 
concat('NTA', ltrim(to_char(1,'0000000000000'))), 
concat('NTA', to_char(1,'0000000000000'))
from dual;

"CONCAT('NTA',TO_CHAR(1,'FM0000000000000'))": "NTA0000000000001" "CONCAT('NTA',LTRIM(TO_CHAR(1,'0000000000000')))": "NTA0000000000001" "CONCAT('NTA',TO_CHAR(1,'0000000000000'))": "NTA 0000000000001"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.