小编典典

SQL 2005是否可以像在case语句中一样使用关键字

sql

我需要一个允许部分匹配的case语句。我收到语法错误,但想知道是否有可能这样做。如果没有,我的第二个解决方案是将其重写为游标…示例:

SELECT CASE ColumnName
           WHEN 'value1' THEN 'answer1'
           WHEN 'value2' THEN 'answer2'
           WHEN LIKE '%TEST%' THEN 'answer3'
       END AS Answer
FROM TableName

阅读 167

收藏
2021-03-23

共1个答案

小编典典

试试这个

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
2021-03-23