Question

For an SQL view I want to concat two fiels of varchar2-type

ROW  |  PNAME | FNAME
1    | JOHN   | DOE
2    | (null) | DOE
3    | JOHN   | (null)
4    | (null) | (null)

The result I want to get is

1  | 'JOHN DOE'
2  | 'DOE'
3  | 'JOHN'
4  | (null)

The best I could achieve so for looks like this

SELECT USER.PNAME || ' ' || USER.FNAME AS "NAME"

But this gives me the following result

1  | 'JOHN DOE'
2  | ' DOE'
3  | 'JOHN '
4  | ' '   

I know why I get this result, but how could I get rid of the obsolete blank that I don't want to have in the result?

I am using Oracle 11g.

Was it helpful?

Solution

SELECT TRIM(USER.PNAME || ' ' || USER.FNAME) AS "NAME"

OTHER TIPS

You can use:

SELECT DECODE(USER.PNAME,"",USER.PNAME,USER.PNAME || ' ') || USER.FNAME AS "NAME"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top