开发者

T-SQL: CASE statement in WHERE?

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 + '%
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜