iSeries: Retrieve NULL as column value

Date: 2016-07-04

Original source: http://techierg.blogspot.nl/2010/02/retrieve-null-in-select-query-in-db2.html

Retrieve NULL in SELECT Query in DB2

Friends, you might have heard about query like this

SELECT NULL FROM [table name]

Now, if you want to do this in DB2, it will return an error that NULL is not a column in the given table name.

To overcome this, you can use two methods
Use NULLIF function like this

Write query like SELECT NULLIF(1, 1) FROM [table name] which will return a NULL value of column type INTEGER because 1 is integer
If you write query like SELECT NULLIF(”, ”) FROM [table name] that will return a NULL value of column type VARCHAR because ” is a string
Don’t ask me about date. I do not know that with NULLIF function :). Write in comments if you know. I’ll put it here.

Use CAST function using which you can get NULL value of the column type you want

Write query like SELECT CAST(NULL AS INTEGER) FROM [table name] which will return NULL value of column type INTEGER
If the query is SELECT CAST(NULL AS VARCHAR(10)) FROM [table name] that will return NULL value of column type VARCHAR
Similarly SELECT CAST(NULL AS DATE) FROM [table name] that will return NULL value of column type DATE
Hope this helps for some.

2740cookie-checkiSeries: Retrieve NULL as column value