Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

tsql - using CASE in T-SQL in the where clause?

Im trying to use case to vary the value im checking in a where clause but I'm getting the error:

incorrect syntax near the keyword 'CASE'

SQL Server 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is one way to include a case statement in a Where clause:

SELECT * FROM sometable
WHERE 1 = CASE WHEN somecondition THEN 1 
    WHEN someothercondition THEN 2
    ELSE ... END

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...