Case when a value is different to other value, SQL Server

后端 未结 4 1681
[愿得一人]
[愿得一人] 2021-02-18 13:57

I have this table structure for table prices:

CREATE TABLE prices
(
     id int, 
     priceFrom int, 
     priceUp int
);

INSERT INTO prices (id,          


        
4条回答
  •  遇见更好的自我
    2021-02-18 14:19

    You should cast the price fields as string so that SQL understands that you don't want to treat them as numbers and do a mathematical operation:

    select pricefrom, priceup,
    case
    when pricefrom = 0 then null
    when priceFrom <> priceUp then cast(priceFrom as varchar) + ' - ' + cast(priceUp as varchar)
    when priceFrom = priceUp then priceFrom
    end as FinalPrice
    from prices
    

提交回复
热议问题