简体   繁体   English

案例表达式在 sql 查询中无法正常工作

[英]Case expression is not working properly in sql query

I want to concat columns of supplier table with comma separator and put it into an alias field named 'contact'.我想用逗号分隔符连接supplier表的列,并将其放入名为“联系人”的别名字段中。 I have used cases for checking null values.我已经使用案例来检查空值。 Suppose if contact_number2 is null then contact_number3 will be in alias field and vice versa.假设如果contact_number2为空,则contact_number3将在别名字段中,反之亦然。 Here is my query这是我的查询

SELECT supplier_Name, supplier_Address, supplier_reference, contact_Number1,
       contact_number2, contact_number3,
      (case when contact_number2 is null then contact_number3 
            when contact_number3 is null then contact_number2 
            when contact_number3 is null and contact_number2 is null then 0
         -- when contact_number2 is not null and contact_number3 is not null then  CONCAT(CONCAT(contact_number2,','), contact_number3)
       end) as contact
FROM SUPPLIER

If I use the fourth condition then it works but if I use multiple condition then it doesn't work.The error is ORA-00932: inconsistent datatypes: expected NUMBER got CHAR 00932. 00000 - "inconsistent datatypes: expected %s got %s"如果我使用第四个条件,那么它可以工作,但是如果我使用多个条件,那么它就不起作用。错误是ORA-00932: inconsistent datatypes: expected NUMBER got CHAR 00932. 00000 - "inconsistent datatypes: expected %s got %s"

The third case is expecting a VARCHAR and you are providing an INT because of which it is returning an error.第三种情况需要一个 VARCHAR 并且您提供一个 INT ,因此它返回错误。 Change was that I replaced 0 with '0'.变化是我用'0'替换了0。 Try this:尝试这个:

SELECT supplier_Name,supplier_Address,supplier_reference,contact_Number1,contact_number2, contact_number3,   

  (case 
   when contact_number2 is null then contact_number3 
    when contact_number3 is null then contact_number2 
    when contact_number3 is null and contact_number2 is null then '0'
  when contact_number2 is not null and contact_number3 is not null then  CONCAT(CONCAT(contact_number2,','), contact_number3)
   end)

   as contact

I think you're after something like this:我认为你在追求这样的事情:

select supplier_name,
       supplier_address,
       supplier_reference,
       contact_number1,
       contact_number2,
       contact_number3,
       case when contact_number2 is not null and contact_number3 is not null then contact_number2||','||contact_number3
            when contact_number3 is null and contact_number2 is null then '0'
            when contact_number2 is null then to_char(contact_number3)
            when contact_number3 is null then to_char(contact_number2)
       end as contact
from   supplier;

Note that case expressions stop at the first condition that is met, so you should make sure that the conditions are in the right order.请注意,case 表达式在满足的第一个条件处停止,因此您应该确保条件的顺序正确。 (Eg. in my query, by the time you get to the "when contact_number2 is null then contact_number3" we already know that contact_number3 can't be null due to the previous condition.) (例如,在我的查询中,当您到达“when contact_number2 为 null then contact_number3”时,我们已经知道由于先前的条件,contact_number3 不能为 null。)

Also, I have converted your CONCAT into the much more common (and more flexible) ||此外,我已将您的CONCAT转换为更常见(且更灵活)的|| form.形式。 With CONCAT , you can only concatenate 2 things at a time, whereas you can have multiple ||使用CONCAT ,您一次只能连接两件事,而您可以有多个|| s to join various strings together. s 将各种字符串连接在一起。

The reason why you got the error you did is because when you concatenate two numbers together (especially when you add a comma into the mix!) the result will be a string.出现错误的原因是当您将两个数字连接在一起时(尤其是当您在混合中添加逗号时!)结果将是一个字符串。 CASE expressions like you to use the same datatype for the result of each condition. CASE 表达式就像您对每个条件的结果使用相同的数据类型一样。

You have to cast numeric data as character before concat.您必须在 concat 之前将数字数据转换为字符。

A case expression's all return values must be type compatible: case 表达式的所有返回值必须是类型兼容的:

SELECT supplier_Name, supplier_Address, supplier_reference, contact_Number1,
       contact_number2, contact_number3,
       case when contact_number2 is not null and contact_number3 is not null
                then CONCAT(CONCAT(cast(contact_number2 as varchar(15)),','),
                            cast(contact_number3 as varchar(15)))
            else cast(coalesce(contact_number2, contact_number3, 0) as varchar(15))
       end as contact
FROM SUPPLIER

Use this用这个

  SELECT supplier_Name, supplier_Address, supplier_reference, contact_Number1,
     contact_number2, contact_number3,
     case when contact_number2 is null then contact_number3 
          when contact_number3 is null then contact_number2 
          when contact_number3 is null and contact_number2 is null then '0'
          when contact_number2 is not null and contact_number3 is not null then contact_number2||','||contact_number3
     end as contact
  FROM SUPPLIER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM