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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DB2 SELECT / UNION - substitute null field

Status
Not open for further replies.

catalpa

Technical User
Oct 8, 2004
3
IT
SELECT TABLEA.CODE, TABLEA.DATE, TABLEA.FIELD
FROM TABLEA
where TABLEA.CODE = 'XX'
UNION all
SELECT TABLEB.CODE, TABLEB.XXDATE AS DATE, TABLEB.FIELD
FROM TABLEB
where TABLEB.CODE <> 'ZZ'
UNION all
SELECT TABLEC.CODE, TABLEC.DATE, TABLEC.FIELD
FROM TABLEC
where TABLEC.FIELD= "YY"

TABLEB has 2 differents date fields:
XXDATE - most important but may be null,
ZZDATE - always significant.

Someone will be able to say me how to set - for TABLEB - field DATE with XXDATE or, if null, with ZZDATE?
TABLEA has only one date, I can't insert another date field ...

Thanks in advance
 
I think Coalesce should do the trick.

SELECT TABLEA.CODE, TABLEA.DATE, TABLEA.FIELD
FROM TABLEA
where TABLEA.CODE = 'XX'
UNION all
SELECT TABLEB.CODE, coalesce(TABLEB.XXDATE,tableb.zzdate) AS DATE, TABLEB.FIELD
FROM TABLEB
where TABLEB.CODE <> 'ZZ'
UNION all
SELECT TABLEC.CODE, TABLEC.DATE, TABLEC.FIELD
FROM TABLEC
where TABLEC.FIELD= "YY"

TABLEB has 2 differents date fields:
XXDATE - most important but may be null,
ZZDATE - always significant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top