Menu

Post image 1
Post image 2
1 / 2
0

IFNULL vs NULLIF in GBase 8a: Why They Are Not Interchangeable

DEV Community·Michael·21 days ago
#d1RliCMN
#gbase#database#software#coding#null#nullif
Reading 0:00
15s threshold

Both IFNULL and NULLIF accept two arguments, but they perform opposite operations. Knowing the difference is key to avoiding subtle data errors. Function Comparison Function Syntax Purpose Logical Equivalent IFNULL IFNULL(expr1, expr2) Replace NULL: if expr1 is NULL , return expr2 ; otherwise return expr1 . CASE WHEN expr1 IS NULL THEN expr2 ELSE expr1 END NULLIF NULLIF(expr1, expr2) Nullify equality: if expr1 equals expr2 , return NULL ; otherwise return expr1 . CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END IFNULL converts NULL → value NULLIF converts value → NULL Why NULLIF Cannot Replace IFNULL Suppose a country column contains NULL s and you want to show 'Unknown' for those rows. Correct usage : SELECT IFNULL ( country , 'Unknown' ) FROM worldcup ; Enter fullscreen mode Exit fullscreen mode Result: NULL → 'Unknown' ; 'China' stays 'China' . Incorrect swap : SELECT NULLIF ( country , 'Unknown' ) FROM worldcup ; Enter fullscreen mode Exit fullscreen mode Logic: If country = 'Unknown' , return NULL .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More