我正在尝试运行这个简单的查询,但它甚至不解析说
消息156,级别15,状态1,行2关键字附近的语法不正确 '之间'。
查询是:
select
case DMProject.dbo.TypeFourTraining.Column2
when Column2 between 181 and 360 then '181-360'
when Column2 between 0 and 180 then '0-180'
END as score
from DMProject.dbo.TypeFourTraining
同样不适用于Column2 < 360
语法..
我从msdn和其他一些网站上网搜索但是我看到我的语法似乎是有效的,那么要么有一个我需要知道的细节,或者有一些我看不到的东西:(
有人可以建议一个解决方案吗?
答案 0 :(得分:4)
您不能混用 simple
和 searched
类型的CASE表达式。删除CASE后指定的字段名DMProject.dbo.TypeFourTraining.Column2
。
SELECT CASE
WHEN Column2 between 181 AND 360 THEN '181-360'
WHEN Column2 between 0 AND 180 THEN '0-180'
END as score
FROM DMProject.dbo.TypeFourTraining
CASE表达有两种类型,即Simple
和Searched
。您无法在同一表达式中组合简单和搜索。
CASE input
WHEN 1 THEN 'a'
WHEN 2 THEN 'b'
WHEN 3 THEN 'c'
ELSE ''
END
CASE
WHEN input = 1 THEN 'a'
WHEN input = 2 THEN 'b'
WHEN input = 3 THEN 'c'
ELSE ''
END
这涉及多个列。您可以在每个WHEN语句中添加多个列。
CASE
WHEN input = 1 AND second_column = 2 THEN 'a'
WHEN input = 2 AND third_column = 3 THEN 'b'
WHEN input = 3 AND (second_column = 4 OR third_column = 6) THEN 'c'
ELSE ''
END
答案 1 :(得分:3)
您正在混淆case语句的两种形式。试试这个:
select (case when Column2 between 181 and 360 then '181-360'
when Column2 between 0 and 180 then '0-180'
END) as score
from DMProject.dbo.TypeFourTraining
另一种形式将用于单身人士价值:
select case DMProject.dbo.TypeFourTraining.Column2
when 1 then '1'
when 2 then '2'
etc. etc. etc.
END as score
from DMProject.dbo.TypeFourTraining