简体   繁体   English

SQL 数据库中的搜索列忽略特殊字符

[英]Search column in SQL database ignoring special characters

Does anybody know if it's possible to do a %LIKE% search against a column in a SQL Server database but get it to ignore any special characters in the column?有谁知道是否可以对 SQL Server 数据库中的列进行 %LIKE% 搜索,但让它忽略列中的任何特殊字符?

So, for example if I have a column called "songs" and they contain the following...因此,例如,如果我有一个名为“歌曲”的列并且它们包含以下内容...


Black Or White黑或白

No Sleep 'till Brooklyn直到布鲁克林不眠

The Ship Song船歌

Papa Don't Preach爸爸不说教


If the user searches for "no sleey till brooklyn" then I would like it to return a match even though they forgot to include the apostrophe.如果用户搜索“no sleey until brooklyn”,那么即使他们忘记包含撇号,我也希望它返回匹配项。 I would also like it to return the 4th row if they search for "SOUL".如果他们搜索“SOUL”,我也希望它返回第 4 行。 I'm sure you get the idea....我相信你明白了......

Any help would really be appreciated.任何帮助将不胜感激。

I would look into using a Full Text Index and then you can use the power of FREETEXT and CONTAINS to do your search.我会考虑使用全文索引,然后您可以使用 FREETEXT 和 CONTAINS 的功能进行搜索。

EDIT: I would still look into refining the Full Text Index searching, however, to follow on from another answer, this is an option using REPLACE.编辑:我仍然会考虑完善全文索引搜索,但是,从另一个答案开始,这是使用 REPLACE 的一个选项。

SELECT
    Artist,
    Title
FROM
    Songs
WHERE
    REPLACE(REPLACE(REPLACE(Artist, '#',''), '*', ''), '"', '') LIKE '%Keywords%'

You will have various characters to remove.您将需要删除各种字符。 Single quotes, double quotes, hyphens, dots, commas, etc.单引号、双引号、连字符、点、逗号等。

You can use Regular expressions in your where clause and do a match on the clean value.您可以在 where 子句中使用正则表达式并对干净值进行匹配。 Read more about regex within SQL here .在此处阅读有关 SQL 中正则表达式的更多信息

As for the art where you want to return the 4th row for SOUL.. you will need aa data structure to tag songs and you will have to search on the tags for the match.至于你想为 SOUL 返回第 4 行的艺术......你需要一个数据结构来标记歌曲,你必须在标签上搜索匹配。 I'm afraid we will need more details on your data structure for that.恐怕我们需要有关您的数据结构的更多详细信息。

使用TRANSLATE、UPPER 和 TRIM的组合。

This is an old question but I just stumbled upon it and am also working with song titles and want to expand upon the accepted answer that uses REPLACE .这是一个老问题,但我只是偶然发现了它,并且还在处理歌曲名称,并希望扩展使用REPLACE的公认答案。 You can create a list of the characters you want to ignore and create a simple function in any language to generate the quick'n'dirty never-ending REPLACE lines.你可以创建一个你想要忽略的字符列表,并用任何语言创建一个简单的函数来生成quick'n'dirty 永无止境的REPLACE行。 For example, in Python:例如,在 Python 中:

def sanitize(db_field):
    special_chars = ['•', '"', "\\'", '*', ',']
    sanitized = "REPLACE({}, '{}', '')".format(db_field, special_chars.pop(0))
    for s in special_chars:
       sanitized = "REPLACE({}, '{}', '')".format(sanitized, s)
    return sanitized

A call such as sanitize("name") will return诸如sanitize("name")类的调用将返回

REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(name, '•', ''), '"', ''), '\'', ''), '*', ''), ',', '')

which can be used in your query.可以在您的查询中使用。 Just wrote this so hope it helps someone.刚刚写了这个,所以希望它可以帮助某人。

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

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