Let us assume we have a table called “ITEM” and it has four fields named “ order_no”, “item_no”, “qty” and “style_sample_no” . In the field “style_sample_no” there is no data i.e. this filed is blank/null or empty.
Check the image (“style_sample_no” is empty)
So, if you want to implement the condition “ If style_sample_no==null then replace style_sample_no with 0 i.e. style_sample_no=0 ” you can do it easily with SQL Query without any IF Condition.
Here is the Solution (Check the image below)
By using COALESCE(i.style_sample_no,0) you can easily replace the blank field of style_sample_no of ITEM table with 0’s or “1”s without using any IF condition in the SQL query.
select i.order_no,i.item_no,i.qty, COALESCE(i.style_sample_no,0)as sample from item i
How To : Postgre SQL Issues about using COALESCE function
COALESCE – it’s just alias to CASE, and CASE is alias for IF ELSE-IF ELSE so implicit you still use in conditional flows 🙂 The goal is do not use in flows, coz’ it reduce performance.
if you just need {0,1} or {true, false} try compare to NULL – it’s comparison and not conditional flows, i.e:
SELECT *,
aColumn is NULL — here no flows , i.e. IF (cond) so do this and this, ELSE do this
FROM aTable;
LikeLike