如何使用通用 SQL 查询搜索 [ 字符?

huangapple go评论54阅读模式
英文:

How to search for [ character using generic sql query?

问题

我有一个表中的字符串列,其中包含 [ 和 ] 字符。我想使用 SQL 查询来搜索它们。我尝试使用以下查询:

SELECT * from EM_TAGS WHERE TAG like '%[[]%';
SELECT * from EM_TAGS WHERE TAG like '%\\[%';

但是它们都没有返回任何行,尽管数据中确实存在这些字符。请问正确的查询是什么?它必须是通用的,因为这个数据库需要移植到所有主要的数据库,如 Oracle、MySQL 等。

英文:

I have a string column in a table that has [ and ] characters. I want to search them using a SQL query. I tried using queries

SELECT * from EM_TAGS WHERE TAG like '%[%';
SELECT * from EM_TAGS WHERE TAG like '%[[%';
SELECT * from EM_TAGS WHERE TAG like '%\[%';

But none of them return any rows although they are there. What is correct query. It has to be generic since this database is to be ported to all major databases like Oracle, MySQL etc.

答案1

得分: 1

你必须在你的 like 语句中使用 ESCAPE。例如:

SELECT * FROM EM_TAGS WHERE TAG LIKE '%\\[%' ESCAPE '\\';

而且你可能正在寻找类似这样的内容,它查找具有开放和关闭括号的字符串,如 '%[%]%':

SELECT * FROM EM_TAGS WHERE TAG LIKE '%\\[%\\]%' ESCAPE '\\';
英文:

you have to use ESCAPE with your like statement. for example :

SELECT * from EM_TAGS WHERE TAG LIKE '%\\[%' ESCAPE '\\'

and probably you are looking for this that looks for string that has opening and closing brackets like '%[%]%':

SELECT * from EM_TAGS WHERE TAG LIKE '%\\[%\\]%' ESCAPE '\\'


</details>



huangapple
  • 本文由 发表于 2023年6月9日 10:15:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定