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 > Coalesce?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-21-07, 05:43
raysefo raysefo is offline
Registered User
 
Join Date: Dec 2006
Posts: 17
Coalesce?

Hi,

I m trying to use coalesce but i m getting this error below, btw i m using db2 8.2.
what i m doing is;
...
where eh.toblm = coalesce(NULL,eh.toblm)

SQL0206N "NULL " is not valid in the context where it is used.

Actually what i want to do is, get the result of this select statement either @id is NOT NULL or NULL! if @id is NULL then all results should be displayed!
...
where eh.toblm = coalesce(@id,eh.toblm)
Reply With Quote
  #2 (permalink)  
Old 01-21-07, 10:10
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I suggest you post the entire query along with the table definition. And what is "@id"?
Reply With Quote
  #3 (permalink)  
Old 01-21-07, 10:55
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
The keyword NULL can only be used in a context where its data type is known.

So, it's e.g. valid to use it with INSERT, as in
Code:
INSERT INTO mytable (col1, col2) VALUES ('value1', NULL)
You may use it in any other context where a constant value is valid, but in that case only after an explicit type cast, e.g.
Code:
CAST(NULL as INT)
or
Code:
CAST(NULL as CHAR(20))
(or of course alternatively with the "old" casting methods INT(NULL) or CHAR(NULL,20)).

Your condition
Code:
WHERE eh.toblm = coalesce(expr, eh.toblm)
makes sense, and will indeed be a "no-op", i.e., match everything, when expr has the NULL value. But it should be of a datatype which is compatible with eh.toblm for that expression to be accepted by DB2, so plugging in NULL for expr won't work while e.g. CAST(NULL as INT) will work (provided eh.toblm is of a numeric type).
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #4 (permalink)  
Old 01-21-07, 14:14
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Peter,

While you are right about the use of NULL, the problem of the original poster lies elsewhere. "where eh.toblm = coalesce(cast(NULL as whatever),eh.toblm)" is equivalent to "where eh.toblm = eh.toblm", which is equivalent to not having the WHERE clause at all.

Apart from using the tool (COALESCE) improperly, he (or she) seems to be using the wrong tool for the task.
Reply With Quote
  #5 (permalink)  
Old 01-21-07, 14:48
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
My understanding of the question was rather whether the condition
Code:
WHERE eh.toblm = coalesce(expr, eh.toblm)
would indeed be "always true" in case expr is NULL. And to test this, raysefo tried to replace expr by NULL, which didn't work, hence his question.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #6 (permalink)  
Old 01-22-07, 07:52
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
" WHERE eh.toblm = coalesce(expr, eh.toblm)
would indeed be "always true" in case expr is NULL "

I disagree. If eh.toblm is <NULL> the result would be "unknown"
Reply With Quote
  #7 (permalink)  
Old 01-22-07, 14:32
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by umayer
If eh.toblm is <NULL> the result would be "unknown"
You're right. My mistake. Thanks!
The following would include that case:
Code:
WHERE coalesce(eh.toblm,'XYXYX') = coalesce(expr, eh.toblm, 'XYXYX')
where you must replace 'XYXYX' by some constant expression of the same datatype as eh.toblm and expr, but which never equals one of the possible values of those.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
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