英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论