Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: CASE types character varying and numeric cannot be matched

I am writing a select statement in Postgres which contains case statement as follows:

,(case when all_loc.country = 'DE' then msc_si.buyer_id else msc_si.buyer_name end) as "purchasing_group_name_buyer_name"  --story
,(case when all_loc.country = 'DE' then msc_si.planner_code else mscp.description end) as "mrp_controller_name"                --story

I am getting the following error. I tried with IS instead of =, didn't work. Without those two case statements the query runs perfectly.

ERROR: CASE types character varying and numeric cannot be matched SQL state: 42804

like image 413
Abhijit Avatar asked Jun 01 '17 21:06

Abhijit


2 Answers

All the branches of a case expression should return the same datatype. One way to achieve that is to explicitly cast where needed:

,(case when all_loc.country = 'DE' then msc_si.buyer_id::varchar else msc_si.buyer_name end) as "purchasing_group_name_buyer_name"
-- Here -----------------------------------------------^
,(case when all_loc.country = 'DE' then msc_si.planner_code::varchar else mscp.description end) as "mrp_controller_name"
-- And here -----------------------------------------------^
like image 70
Mureinik Avatar answered Nov 01 '22 14:11

Mureinik


SELECT "table","schema",
CASE 
WHEN  "size" <= 1024 Then SIZE::varchar || 'MB'
WHEN  "size" > 1024 AND "size"  < 1000000 Then ("SIZE"/1024)::varchar || 'GB'
WHEN  "size" > 1000000 THEN ("SIZE"/1024)/1024::varchar || 'TB'
END size
FROM SVV_TABLE_INFO 
order by 1;
like image 21
Biswajeet Praharaj Avatar answered Nov 01 '22 14:11

Biswajeet Praharaj