Case expression is not working properly in sql query

后端 未结 4 1403
长情又很酷
长情又很酷 2021-01-29 15:20

I want to concat columns of supplier table with comma separator and put it into an alias field named \'contact\'. I have used cases for checking null values. Suppos

相关标签:
4条回答
  • 2021-01-29 15:50

    You have to cast numeric data as character before concat.

    A case expression's all return values must be type compatible:

    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
    
    0 讨论(0)
  • 2021-01-29 15:53

    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
    
    0 讨论(0)
  • 2021-01-29 15:58

    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. (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.)

    Also, I have converted your CONCAT into the much more common (and more flexible) || form. With CONCAT, you can only concatenate 2 things at a time, whereas you can have multiple ||s to join various strings together.

    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.

    0 讨论(0)
  • 2021-01-29 16:16

    The third case is expecting a VARCHAR and you are providing an INT because of which it is returning an error. Change was that I replaced 0 with '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
    
    0 讨论(0)
提交回复
热议问题