Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

USING COALESCE and || in SQL 1

Status
Not open for further replies.

DFW1999

Programmer
Nov 11, 2002
31
0
0
US
Hi,

this following script is part of a fastExport script which I want to execute in TD. Without using COALESCE I am able to process that script with no error. But if one of the concatenated field is NULL then the whole string becomes null and gives me empty fixed length field.

When I tried to check NULL with Coalesce function then I am getting the following error:

When I am trying to use COALESECE, TRIM and || all together I am getting this following error:
"3707: syntax error expecing something like "IN" between ) and ||'




HERE IS MY query:

SELECT
CASE
WHEN coalesce(TRIM(D.col1),'')||
coalesce(TRIM(D.col2),'')||
coalesce(TRIM(D.col3),'')||
coalesce(TRIM(D.col4),'')||
coalesce(TRIM(D.col5),'')||
coalesce(TRIM(D.col6),''))=''
OR coalesce(TRIM(D.col1),'')||
coalesce(TRIM(D.col2),'')||
coalesce(TRIM(D.col3),'')||
coalesce(TRIM(D.col4),'')||
coalesce(TRIM(D.col5),'')||
coalesce(TRIM(D.col6),'') IS NULL THEN
CAST(' ' as CHAR(30))
ELSE
CAST(SUBSTR(coalesce(TRIM(D.col1),'')||
coalesce(TRIM(D.col2),'')||
coalesce(TRIM(D.col3),'')||
coalesce(TRIM(D.col4),'')||
coalesce(TRIM(D.col5),'')||
coalesce(TRIM(D.col6),'')||' ',1,30) as CHAR(30))
END
FROM TABLE_ADDR D

Any help would be appreciated.
thanks

 
You need to place brackets around CASE/COALESCE
(coalesce(...)) || (coalesce (...))

But....
There's no need for the outer CASE at all:

select
CAST(
(coalesce(TRIM(D.col1),''))||
(coalesce(TRIM(D.col2),''))||
(coalesce(TRIM(D.col3),''))||
(coalesce(TRIM(D.col4),''))||
(coalesce(TRIM(D.col5),''))||
(coalesce(TRIM(D.col6),''))
) as CHAR(30))
from tab

Dieter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top