I have the following WHERE clause in my SQL query currently:
WHERE c.Town LIKE '%' + @Town + '%'
What I want do is make the first '%' optional - so if the user has selected to just filter by records that 开发者_JS百科just start with the given text, the WHERE would be
WHERE c.Town LIKE @Town + '%'
I assumed the simplest way to do this would be CASE statements, but I'm having no joy as it seems the syntax is incorrect. Can I get any pointers? Here's the CASE statement I was trying:
WHERE c.Town LIKE (CASE WHEN @FilterOptions = 1 THEN '%' ELSE '') + @Town + '%'
You missed the END:
WHERE c.Town LIKE (CASE WHEN @FilterOptions = 1 THEN '%' ELSE '' END) + @Town + '%'
A CASE
must end with an END
. Try
WHERE c.Town LIKE (CASE WHEN @FilterOptions = 1 THEN '%' ELSE '' END) + @Town + '%'
Case should have end.
I think the below statement can help you.
WHERE c.Town LIKE (CASE WHEN @FilterOptions = 1 THEN '%' ELSE '' END) + @Town + '%
精彩评论