SQL 'LIKE' 查询使用 '%',其中搜索条件包含 '%' [英] SQL 'LIKE' query using '%' where the search criteria contains '%'

查看:60
本文介绍了SQL 'LIKE' 查询使用 '%',其中搜索条件包含 '%'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的 SQL 查询.

I have an SQL query as below.

Select * from table 
where name like '%' + search_criteria + '%' 

如果search_criteria = 'abc',它会返回包含xxxabcxxxx的数据,这很好.

If search_criteria = 'abc', it will return data containing xxxabcxxxx which is fine.

但是如果我的search_criteria = 'abc%',它仍然会返回包含xxxabcxxx的数据,应该不是这种情况.

But if my search_criteria = 'abc%', it will still return data containing xxxabcxxx, which should not be the case.

我该如何处理这种情况?

How do I handle this situation?

推荐答案

如果您希望 search_criteria 中的 % 符号被视为文字字符而不是作为通配符,转义为 [%]

If you want a % symbol in search_criteria to be treated as a literal character rather than as a wildcard, escape it to [%]

... where name like '%' + replace(search_criteria, '%', '[%]') + '%'

这篇关于SQL 'LIKE' 查询使用 '%',其中搜索条件包含 '%'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆