带转换语句的SQL CASE

时间:2019-02-20 17:05:08

标签: sql case

我需要进行选择,将值转换为int并在另一个列中设置一个数字。

Select * ,
       (CASE WHEN City='London' then '2' ELSE '1' END) as Status 
from customers

问题是我想在INT中转换2:

select * ,
       (CASE WHEN City='London' then CAST(City as int) '2' ELSE '1' END) as Status 
from customers

似乎在THEN中仅接受一项操作?

1 个答案:

答案 0 :(得分:0)

您可以尝试

 SELECT  * ,
(CASE WHEN City = 'London' THEN CAST('2' AS INT) ELSE CAST('1' AS INT) END) AS Status 
FROM
customers

 SELECT  * ,
(CASE WHEN City = 'London' THEN 2 ELSE 1 END) AS Status 
FROM
customers

对于将城市名称转换为numeric的情况,看来您在任何世界都无法将城市名称转换为整数似乎是不现实的。