Cast NULL in DB2 SQL statements
14,931
- For the INSERT operations, both will work.
- For the SELECT, the cast is necessary.
- For the stored procedure, I hypothesize that both will work.
The reasoning is relatively simple:
- With the INSERT, the target column (A) tells the system which type the NULL is.
- With the stored procedure, the declaration of the argument tells the system which type the NULL is.
- With the SELECT, there is nothing apart from the cast to tell the system which type the NULL is.
Related videos on Youtube
Author by
Lukas Eder
I am the founder and CEO at Data Geekery, the company behind jOOQ.
Updated on May 13, 2022Comments
-
Lukas Eder almost 2 years
I have found a lot of information about DB2 and its strong type system, but no comprehensive fact sheet about casting NULL. Also, there seem to be subtle differences between versions.
I don't quite understand when I can simply put
null
in a SQL clause, and when I need to explicitly castnull
to any other type.Examples (which one is right?)
Inserting (or updating):
INSERT INTO X (A) VALUES (null) INSERT INTO X (A) VALUES (cast(null as integer))
Selecting:
SELECT null FROM SYSIBM.DUAL SELECT cast(null as integer) FROM SYSIBM.DUAL
Calling (stored procedures):
X (null); X (cast(null as integer));
Etc...
Does anyone know a good resource explaining when casts are necessary? And in what versions of DB2 / With what configuration parameters?
-
Lukas Eder over 13 yearsYour reasoning makes sense and is good news to me. Do you know where that is documented?
-
Jonathan Leffler over 13 yearsThe obvious starting point is DB2 9.7 Info Centre. One piece with some relevant info about changes in NULL handling in 9.7 here. Otherwise, it should be in here: SQL Reference.
-
Lukas Eder over 13 yearsThanks a lot, that's exactly what I was looking for. Especially the fact that IBM changed something with version 9.7 was what I suspected but didn't find...