You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Handling null values in Postgres using COALESCE function
SELECT COALESCE(null); -- nullSELECT COALESCE(null, 1); -- 1, will pick the next parameter which is not nullSELECT COALESCE(null, null, 1); -- 1, will pick the next parameter which is not nullSELECT COALESCE(null, 1, 10); -- 1, -- handle the empty emails in the tableSELECT id,firstname, lastname, gender, COALESCE(email, 'NULL') from person;
SELECT id,firstname, lastname, gender, COALESCE(email, 'NULL') from person;
SELECT id,firstname, lastname, gender, COALESCE(email, 'NULL') AS email from person;
Handling Division by zero exception NULLIF
/* NullIf always returns first paramter if both are different */SELECT10/0; -- throws errorSELECT NULLIF(10, 10); -- will be nullSELECT NULLIF(19, 10); -- 19SELECT10/ NULLIF(12, 90); -- not throws error, but wait what if SELECT10/ NULLIF(0, 10) -- YES, It will throw errorSELECT COALESCE(10/ NULLIF(0, 0), 0) -- Displays default value