简体   繁体   English

从SQL LIKE查询中排除标点符号

[英]Exclude punctuation from SQL LIKE query

I am running a SQL query looking for exact words or phrases (user decides at run time). 我正在运行一个SQL查询,以查找确切的单词或短语(用户在运行时决定)。 So the request might be to look for the phrase "plays catch" 因此,请求可能是寻找短语“ plays catch”

The SQL query that I create would be similar to this: 我创建的SQL查询与此类似:

SELECT * 
FROM NOTES 
WHERE DETAIL LIKE "plays catch %" 
   OR DETAIL LIKE "% plays catch %";

Because the user will be looking for an exact phrase I put the space after "catch" and before the "%" because I want to avoid a similar but different word such as "Catch-up". 因为用户将要查找确切的短语,所以我在“ catch”之后和“%”之前放置空格,因为我想避免使用类似但不同的词,例如“ Catch-up”。

So the problem comes with handling punctuation such as a period or a comma. 因此,问题出在处理标点符号,例如句点或逗号。

My query now unfortunately will exclude a note with the detail of 不幸的是,我的查询现在将排除带有以下内容的注释:

John plays catch.

but will include 但将包括

John plays catch with his dog.

Does anybody know of a simple way to have basic punctuation ignored in a SQL query? 有人知道在SQL查询中忽略基本标点的简单方法吗?

BTW, this is my first post on your forum so happy to accept guidance if I've mis-stepped in this post. 顺便说一句,这是我在您论坛上的第一篇帖子,如果我误导了该帖子,很高兴接受指导。

If your database supports regular expressions, then you can handle it that way. 如果您的数据库支持正则表达式,则可以采用这种方式进行处理。 For instance: 例如:

SELECT *
FROM NOTES 
WHERE ' ' || DETAIL REGEXP ' plays catch[ ,.!?]' ;

( || is the standard operator for string concatenation, although some databases do not support it.) ||是字符串连接的标准运算符,尽管某些数据库不支持它。)

Or you could use the word delimiter ( '\\w' ) if that is appropriate. 或者,如果合适,可以使用定界符( '\\w' )一词。

If you database does not, you can use REPLACE() : 如果数据库不存在,则可以使用REPLACE()

SELECT *
FROM NOTES 
WHERE REPLACE(REPLACE(' ' || DETAIL, ',', ' '), '.', ' ') LIKE '% plays catch %' ;
SELECT *
FROM testing
WHERE alphanum(test) LIKE CONCAT('%', alphanum('plays catch'), '%')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM