Can I have multiple boolean expressions in a searched case sql statement?

北城余情 提交于 2020-01-24 10:54:11

问题


I'm trying to create a searched case sql statement based on a number of boolean expressions.

For example

select CASE a = b OR c = d OR e = f OR g = h
THEN 'x' ELSE 'y' END
from table_name

I keep getting the following error: Incorrect syntax near '='.

Am I doing something that's inherently wrong/illegal in sql, or is this something I can fix? If it is fixable, how can I do it?

Thanks!


回答1:


select CASE when ( a = b OR c = d OR e = f OR g = h )
THEN 'x' ELSE 'y' END
from table_name

The format is CASE..WHEN..THEN..ELSE..END. You were just missing the WHEN.




回答2:


I feel really stupid. I'm missing a when after the case...

Should be

select CASE when a = b OR c = d OR e = f OR g = h
THEN 'x' ELSE 'y' END
from table_name

Then it works...




回答3:


You missed WHEN keyword

SELECT 
CASE WHEN a = b OR c = d OR e = f OR g = h
THEN 'x' 
ELSE 'y' 
END
FROM table_name


来源:https://stackoverflow.com/questions/6877292/can-i-have-multiple-boolean-expressions-in-a-searched-case-sql-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!