错误:运算符不存在:字符变化=布尔值

时间:2018-09-05 13:15:19

标签: sql postgresql case

我有以下查询:

SELECT
CASE
    tbl_appointment_types."name" 
    WHEN tbl_appointment_types."name" LIKE 'Refill-ART' THEN
    'Re-Fill' 
    WHEN tbl_appointment_types."name" = 'Enhanced Adherence'::character varying THEN
    'Enhanced Adherence' 
    WHEN tbl_appointment_types."name" = 'PHARMACY APPOINTMENT'::character varying THEN
    'Re-Fill' ELSE tbl_appointment_types."name" 
END 
FROM
    tbl_appointment INNER JOIN tbl_appointment_types ON tbl_appointment_types."id" = tbl_appointment.app_type_1 
GROUP BY
    tbl_appointment_types."name";

当我尝试运行它时,出现以下错误:

> ERROR:  operator does not exist: character varying = boolean
  LINE 4:  WHEN tbl_appointment_types."name" LIKE 'Refill-ART' THEN
           ^
  HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

> Time: 0.054s

如何解决以下问题?

1 个答案:

答案 0 :(得分:3)

CASE表达式有两种形式:

仅支持等式的“简短”形式,在这种情况下,您首先要声明表达式,然后是要与之进行比较的值(使用=),例如

 CASE some_column
    WHEN 1 THEN 'One'
    WHEN 2 THEN 'Two'
    ELSE 'Something else'
 END

或更详细的版本,您可以为每个WHEN使用不同的条件。但是在那种情况下,您可以在CASE之后加上表达式:

 CASE -- nothing here!
    WHEN some_column = 1 THEN 'One'
    WHEN some_column = 2 THEN 'Two'
    ELSE 'Something else'
 END

因此,您需要将CASE表达式编写为:

CASE -- nothing here!
   WHEN tbl_appointment_types."name" LIKE 'Refill-ART' THEN 'Re-Fill' 
   WHEN tbl_appointment_types."name" = 'Enhanced Adherence' THEN 'Enhanced Adherence' 
   WHEN tbl_appointment_types."name" = 'PHARMACY APPOINTMENT' THEN 'Re-Fill' 
   ELSE tbl_appointment_types."name" 
END 

在没有通配符的情况下使用LIKE确实没有任何意义。 "name" LIKE 'Refill-ART'"name" = 'Refill-ART'相同。

您可能是说"name" LIKE 'Refill-ART%'(请注意末尾的%