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 > SELECT ..., NULL, ... FROM table ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-14-06, 05:47
akaraivanov akaraivanov is offline
Registered User
 
Join Date: Nov 2003
Posts: 42
SELECT ..., NULL, ... FROM table ?

Hi All,

It seems it is not possible to select NULL from a table?
For example:

SELECT null FROM t

produces

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

Can that be true? I can select any other constant but not NULL. I can workaround it
by using the scalar function NULLIF(). Fx

SELECT NULLIF(0,0) FROM T

But that is very bad way of doing it and probably has performance impact.

Is there any good explanantion for all this?

Thanks in advance.

Alex.
Reply With Quote
  #2 (permalink)  
Old 07-14-06, 06:37
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
cast(null as int)
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 07-14-06, 12:43
willyunger willyunger is offline
Registered User
 
Join Date: Dec 2004
Posts: 7
Null

Perhaps what you want to get is

SELECT COL1, COL2,.... WHERE COLX IS NULL
Reply With Quote
  #4 (permalink)  
Old 07-14-06, 14:43
akaraivanov akaraivanov is offline
Registered User
 
Join Date: Nov 2003
Posts: 42
Quote:
Originally Posted by willyunger
Perhaps what you want to get is

SELECT COL1, COL2,.... WHERE COLX IS NULL

I really wanted to get a constant value of NULL for one or more columns. That is useful
for example when I use UNION. For example

( SELECT a.id, b.id, NULL FROM a JOIN b ON a.id = b.a_id )
UNION ALL
( SELECT a.id, NULL, c.id FROM a JOIN c ON a.id = c.a_id)

But instead of typing NULL directly I have to use CAST(NULL as INT) ...
Reply With Quote
  #5 (permalink)  
Old 07-19-06, 10:02
onemanifest onemanifest is offline
Registered User
 
Join Date: Jun 2006
Posts: 11
Why would you need a NULL value? In your example any 'default' would do. If it were an integer field for example, I'd use somthing like -1 as a default 'ID unknown'.

Peter
Reply With Quote
  #6 (permalink)  
Old 07-19-06, 10:30
akaraivanov akaraivanov is offline
Registered User
 
Join Date: Nov 2003
Posts: 42
Quote:
Originally Posted by onemanifest
Why would you need a NULL value? In your example any 'default' would do. If it were an integer field for example, I'd use somthing like -1 as a default 'ID unknown'.

Peter
This is good workaround Of course your default value has to be outside of the range of the normal values for that attribute. Also the application has to check for that default value instead of NULL . NULL is of course more natural choise, but is quite unpleasant to have to cast it to a specific type.
Reply With Quote
  #7 (permalink)  
Old 07-19-06, 13:12
onemanifest onemanifest is offline
Registered User
 
Join Date: Jun 2006
Posts: 11
Yes, you need to keep the ID range in mind. I normally use -1 for 'unknown' and -9 for 'deleted' and a valid ID range > 0 where ever possible when dealing with ID's, so I don't get confused or forget my choices.

Another reason not to use NULL values is that some functions respond in unexpected ways when confronted with NULL values. Sometimes I'd end up converting NULLs into something else further down the line anyway.

Peter
Reply With Quote
  #8 (permalink)  
Old 07-20-06, 04:19
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
I sometimes use null as sathyaram_s suggested and sometimes 'default' value depending on sort I need. Null values are not sorted the same way as 'default' values. So don't ignore posibilities of using null in SQL.
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