case when use example
It would be my great honor if my blog post could help you. Due to my limited ability, I would be very grateful if you could give me some advice on what I have written. In the future, I will try to sort out some of the knowledge I have learned at work and share it with you. Your opinion is that my dynamic
statement has two ways: 1. case field name
when field value then 'custom value'
when field value 2 then 'custom value 2'
....
else 'custom value n'
end
2. case when field name=field value then 'custom value'
when field name 2=field value 2 then 'custom value 2'
....
else 'custom value n'
end
select (case d_id
when 16 then ' Finance Department'
when 18 then 'Engineering Department'
when 19 then 'Technical Department'
when 25 then 'Marketing Department'
else 'Other Departments'
end) d_name
from tb_dept ;
select (case when d_id=16 then 'Finance Department'
when d_id=18 then 'Engineering Department'
when d_id=19 then 'Technical Department'
when d_id =22 then 'planning department'
when d_id=25 then 'marketing department'
else 'other departments'
end) d_name
from tb_dept;
SQL query result:
statement has two ways: 1. case field name
when field value then 'custom value'
when field value 2 then 'custom value 2'
....
else 'custom value n'
end
2. case when field name=field value then 'custom value'
when field name 2=field value 2 then 'custom value 2'
....
else 'custom value n'
end
select (case d_id
when 16 then ' Finance Department'
when 18 then 'Engineering Department'
when 19 then 'Technical Department'
when 25 then 'Marketing Department'
else 'Other Departments'
end) d_name
from tb_dept ;
select (case when d_id=16 then 'Finance Department'
when d_id=18 then 'Engineering Department'
when d_id=19 then 'Technical Department'
when d_id =22 then 'planning department'
when d_id=25 then 'marketing department'
else 'other departments'
end) d_name
from tb_dept;
SQL query result: