I am facing an issue using CASE expression in where clause, To my knowledge syntax is correct but getting a error. Below is my code:
Where
CASE
WHEN (@ItemFor='' and @ItemTo='')
THEN id like '%'
ELSE id between @ItemFor and @ItemTo
END
Above code looks correct to me but I am getting syntax error saying
Incorrect syntax near the keyword 'like'.
Edit
I have 4 sets of parameters and all need to go to where clause and all are string values:
WHERE
CASE
WHEN (@ItemFor='' and @ItemTo='')
THEN id like '%'
ELSE id between @ItemFor and @ItemTo
END
AND
CASE
WHEN (@CodeFrom='' and @CodeTo='')
THEN Code like '%'
ELSE code between @CodeFrom and @CodeTo
END