I want to check the column mr.name, if mr.name is null then i have to replace mr.name as mr.ticket_no. How? Can use if else or case?
select ROW_NUMBER() OVER(ORDER BY mr_user) sl_no,* from (select mr.name as mr_no, coalesce(mr.user_id,0) as mr_user from stock_production_lot lot left join kg_grn grn on (grn.name = lot.grn_no) left join kg_department_indent mr on (mr.name = grn.mr_no) order by mr.user_id) main where mr_user=65
When i use like this
case when mr.name is null then '' else mr.ticket_no = grn.mr_no as mr_no
it will throw an error
if mr.name = null means i have to replace mr.name = mr.ticket_no. I want to check the column mr.name, if mr.name is null then i have to replace mr.name as mr.ticket_no
Advertisement
Answer
You can use coalesce() to replace null with anything:
select ROW_NUMBER() OVER(ORDER BY mr_user) sl_no,* from (select coalesce(mr.name,mr.ticket_no) as mr_no, coalesce(mr.user_id,0) as mr_user from stock_production_lot lot left join kg_grn grn on (grn.name = lot.grn_no) left join kg_department_indent mr on (mr.name = grn.mr_no) order by mr.user_id) main where mr_user=65
But if you are comfortable with case when then use as below:
select ROW_NUMBER() OVER(ORDER BY mr_user) sl_no,* from (select (case when mr.name is null then mr.ticket_no else mr.name end) as mr_no, coalesce(mr.user_id,0) as mr_user from stock_production_lot lot left join kg_grn grn on (grn.name = lot.grn_no) left join kg_department_indent mr on (mr.name = grn.mr_no) order by mr.user_id) main where mr_user=65