Case expression is not working properly in sql query

后端 未结 4 1476
长情又很酷
长情又很酷 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 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
    

提交回复
热议问题