I have a parameter in a stored procedure called @name. If the length of this parameter is 1, a single column (LastName) needs to be searched, else two columns (FirstName & LastName) need to be searched.
Here’s what I have so far –
WHERE
p.Year = @year
AND
(CASE WHEN LEN(@name) = 1 THEN (p.LastName LIKE @name + '%')
ELSE (p.LastName LIKE @name + '%' OR p.FirstName LIKE @name + '%')
END)
It gives me this error
Incorrect syntax near the keyword ‘like’.`
How do I accomplish this?
You can do
But breaking this up into two separate queries and
IF ... ELSEconditional logic may well give you a better plan rather than trying one catch all query for both cases.