18

我试图用案例来改变我在 where 子句中检查的值,但我得到了错误:

关键字“CASE”附近的语法不正确

SQL 服务器 2005

select * 
from   table
where  ((CASE when adsl_order_id like '95037%'
         then select '000000'+substring(adsl_order_id,6,6)
         ELSE select adsl_order_id
       END)
       not in (select mwebID from tmp_csv_dawis_bruger0105)
4

6 回答 6

39

这是在 Where 子句中包含 case 语句的一种方法:

SELECT * FROM sometable
WHERE 1 = CASE WHEN somecondition THEN 1 
    WHEN someothercondition THEN 2
    ELSE ... END
于 2010-01-05T13:27:35.087 回答
4

你可以试试

SELECT *
FROM table
WHERE (SELECT CASE WHEN adsl_order_id LIKE '95037%'
              THEN '000000' + SUBSTRING(adsl_order_id, 6, 6)
              ELSE adsl_order_id
              END)
      NOT IN (select mwebID from tmp_csv_dawis_bruger0105)
于 2010-01-05T13:26:34.170 回答
1

相关子查询是一种可能性:

select * 
from mytable
where not exists (
    select * 
    from 
        tmp_csv_dawis_bruger0105
    where 
        mwebID = 
        CASE when mytable.adsl_order_id like '95037%' then '000000' + substring(mytable.adsl_order_id,6,6)
        ELSE mytable.adsl_order_id END
 )
于 2010-01-05T14:27:56.580 回答
0

我来这个问题是为了寻找答案,认为 WHERE CASE ... 将是解决方案。我无法调整我的问题的答案,但这种使用可能为空、部分或特定参数的技术有效:

CREATE PROC myproc  @vJobID VARCHAR (11) 
AS
SELECT * FROM Jobs 

WHERE Jobs.JobID like coalesce(@vJobID+'%','%') 

高温高压

于 2021-01-13T19:39:20.413 回答
-1

CASE 表达式之前的左括号过多。

于 2010-01-08T17:51:03.850 回答
-1

把它放在 SELECT 子句中......

select *, (CASE when adsl_order_id like '95037%'
         then '000000'+substring(adsl_order_id,6,6)
         ELSE adsl_order_id
       END) AS Id
from   table
where  not in (select mwebID from tmp_csv_dawis_bruger0105)

此外,您不需要“选择”CASE 的结果。

于 2017-10-10T10:43:16.887 回答