我需要一个允许部分匹配的case语句.我得到一个语法错误,但是我想知道是否有可能类似的事情.如果没有,我的第二个解决方案是重写为光标…
例:
例:
SELECT CASE ColumnName WHEN 'value1' THEN 'answer1' WHEN 'value2' THEN 'answer2' WHEN LIKE '%TEST%' THEN 'answer3' END AS Answer FROM TableName
解决方法
尝试这个
SELECT CASE WHEN ColumnName = 'value1' THEN 'answer1' WHEN ColumnName = 'value2' THEN 'answer2' WHEN ColumnName LIKE '%TEST%' THEN 'answer3' END AS Answer FROM TableName
你可以运行的例子
SELECT name,CASE WHEN Name = 'sysobjects' THEN 'answer1' WHEN Name = 'syscols' THEN 'answer2' WHEN Name LIKE '%p%' THEN 'answer3' ELSE 'unknown' END AS Answer FROM sysobjects