DB2: Won't Allow "NULL" column?

GyRo picture GyRo · Mar 24, 2010 · Viewed 22.8k times · Source

Part of a complex query that our app is running contains the lines: ...(inner query)

SELECT
...
NULL as column_A,
NULL as column_B,
...
FROM
...

This syntax of creating columns with null values is not allowed in DB2 altough it is totally OK in MSSQL and Oracle DBs. Technically I can change it to:

'' as column_A,
'' as column_B, 

But this doesn't have exactly the same meaning and can damage our calculation results. How can I create columns with null values in DB2 using other syntax??

Answer

Ian Bjorhovde picture Ian Bjorhovde · Mar 24, 2010

DB2 is strongly typed, so you need to tell DB2 what kind of column your NULL is:

select 
   ...
   cast(NULL as int) as column_A,
   cast(NULL as varchar(128)) as column_B,
   ...
FROM
   ...