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 > How can I select NULL values when I create view?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-23-06, 08:03
zhouhaiming zhouhaiming is offline
Registered User
 
Join Date: Jan 2003
Posts: 74
How can I select NULL values when I create view?

Hello everyone!
I want to create a view like this:

create view a(f1, f2, f3, ...) as
(select a.f1, b.f2, NULL, ...
from table1 a, table2 b, ...)

But DB2 raised the error:
SQL0206N "NULL" is not valid in the context where it is used. SQLSTATE=42703
Reply With Quote
  #2 (permalink)  
Old 09-23-06, 10:22
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
null

why do you want to select a null value from a table
select '' from table --- would also work
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #3 (permalink)  
Old 09-24-06, 03:49
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
try:

create view a(f1, f2, f3, ...) as
(select a.f1, b.f2, NULLIF(1,1), ...
from table1 a, table2 b, ...)
Reply With Quote
  #4 (permalink)  
Old 09-24-06, 14:50
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by zhouhaiming
(select a.f1, b.f2, NULL, ...
from table1 a, table2 b, ...)
The only reason that you cannot just SELECT NULL is that NULL has no datatype and every column must have a datatype.

NULLIF(1,1) returns NULL of datatype INT (since 1 is an INT constant);
likely, NULLIF('','') returns NULL of datatype VARCHAR.

For a more generic way of having NULL of a certain datatype (like e.g. DATE) you will have to use CAST:
Code:
SELECT ..., Cast(NULL as DATE), ...
FROM table1 ...
(or replace "DATE" by any datatype you want).
__________________
--_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
  #5 (permalink)  
Old 09-24-06, 21:12
zhouhaiming zhouhaiming is offline
Registered User
 
Join Date: Jan 2003
Posts: 74
Yes, Peter.Vanroose is right! I just test, it's ok! Thank you for your help!
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