vendredi 8 juillet 2016

How to use case statment in where condition(sql)?

I try to write a query with sql that get one parameter and use this parameter in where section of below query but get error: I want do this :

if @state equal with 2 Check in where condition @state=2

if @state equal with 3 Check in where condition @state=3

if @state equal with 0 then don't Check in where condition

I try to write this statement with case but get syntax error

SELECT *
FROM   tbl1
WHERE  @StartDate <= Cast([date] AS DATE)
       AND Cast([date] AS DATE) <= @endDate
       AND [reservetype] = @Type
       AND  CASE
                       WHEN @State = 2 THEN [state]=2
                       WHEN @State = 3 THEN [state]=3
                       WHEN @State = 0 then ....
                     END  

Aucun commentaire:

Enregistrer un commentaire