If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Probs with case expression

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-07-04, 05:28
ulirebmann ulirebmann is offline
Registered User
 
Join Date: Apr 2004
Posts: 2
Probs with case expression

hi all,

i want to make following:

field1 + field2 + field3

but only when these fields were not NULL !

i tried as follows:

......
CASE WHEN b.F1 IS NULL THEN 0 ELSE b.F1 END,
CASE WHEN b.F2 IS NULL THEN 0 ELSE b.F2 END,
CASE WHEN b.F3 IS NULL THEN 0 ELSE b.F3 END,
(b.F1+b.F2+b.F3) as XXX, b.F1, b.F2, b.F3,.....

the result column XXX is NULL for each row.

what to to ?
Reply With Quote
  #2 (permalink)  
Old 04-07-04, 06:38
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Re: Probs with case expression

does this help ?


CASE WHEN b.F1 IS NULL THEN 0 ELSE b.F1 END+
CASE WHEN b.F2 IS NULL THEN 0 ELSE b.F2 END+
CASE WHEN b.F3 IS NULL THEN 0 ELSE b.F3 END as XXX,
b.F1, b.F2, b.F3

Sathyaram

Quote:
Originally posted by ulirebmann
hi all,

i want to make following:

field1 + field2 + field3

but only when these fields were not NULL !

i tried as follows:

......
CASE WHEN b.F1 IS NULL THEN 0 ELSE b.F1 END,
CASE WHEN b.F2 IS NULL THEN 0 ELSE b.F2 END,
CASE WHEN b.F3 IS NULL THEN 0 ELSE b.F3 END,
(b.F1+b.F2+b.F3) as XXX, b.F1, b.F2, b.F3,.....

the result column XXX is NULL for each row.

what to to ?
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 04-07-04, 07:11
ulirebmann ulirebmann is offline
Registered User
 
Join Date: Apr 2004
Posts: 2
Re: Probs with case expression

Quote:
Originally posted by sathyaram_s
does this help ?
Sathyaram

...and how it does :-)

thx a lot - you 're great!
Reply With Quote
  #4 (permalink)  
Old 04-07-04, 08:57
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You can also use coalesce:


COALESCE(b.F1,0)+coalesce(b.F2,0)+coalesce(b.F3,0) as XXX,


Makes it a litter easier to read.

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On