如何在sql server 2005中使用具有多个值的like条件?
我需要根据 nvarchar(1000) 列中的一些文本匹配来过滤记录。 表有超过 40 万条记录并且还在不断增长。现在,我正在使用 Like 条件:-
SELECT
*
FROM
table_01
WHERE
Text like '%A1%'
OR Text like '%B1%'
OR Text like '%C1%'
OR Text like '%D1%'
有什么首选的解决方法吗?
I need to filter out records based on some text matching in nvarchar(1000) column.
Table has more than 400 thousands records and growing. For now, I am using Like condition:-
SELECT
*
FROM
table_01
WHERE
Text like '%A1%'
OR Text like '%B1%'
OR Text like '%C1%'
OR Text like '%D1%'
Is there any preferred work around?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将检查文本是否包含 A1、B1、C1、D1...
参考使用 SQL Server 中的类似条件
This will check if the texts contains A1, B1, C1, D1, ...
Reference to using the Like Condition in SQL Server
如果您知道子字符串的确切位置,您可以尝试以下操作:
You can try the following if you know the exact position of your sub string:
请查看 msdn 上的LIKE。
您可以通过将更多详细信息合并到单个 LIKE 子句中来减少过滤器的数量。
Have a look at LIKE on msdn.
You could reduce the number filters by combining more details into a single LIKE clause.
如果您可以在表的该列上创建一个全文索引(假设对性能和空间进行了大量研究),那么您可能会看到文本匹配性能的巨大改进。您可以访问此链接查看
全文搜索< /代码>是
以及此链接,了解如何创建
全文索引< /代码>。
If you can create a
FULLTEXT INDEX
on that column of your table (that assumes a lot of research on performance and space), then you are probably going to see a big improvement on performance on text matching. You can go to this link to see whatFULLTEXT SEARCH
isand this link to see how to create a
FULLTEXT INDEX
.我需要这样做,以便可以在 SQL Server Profiler 跟踪模板中的
DatabaseName
列的过滤器中允许使用两个不同的数据库。您所能做的就是填写
Like
子句的正文。使用 John Hartscock 的回答中的参考,我发现 like 子句使用了一种有限的正则表达式模式。
对于OP的场景,MSMS有解决方案。
假设我想要数据库 ABCOne、ABCTwo 和 ABCThree,我为每个字符提出了本质上独立的白名单:
它可以轻松扩展到任何字符串集。它不会是铁定的,最后一个模式也将匹配 ABCOwe、ABCTnr 或 ABCOneHippotamus,但如果您要过滤一组有限的可能值,那么您很有可能让它发挥作用。
您也可以使用
[^]
运算符来显示不可接受字符的黑名单。I needed to do this so that I could allow two different databases in a filter for the
DatabaseName
column in an SQL Server Profiler Trace Template.All you can do is fill in the body of a
Like
clause.Using the reference in John Hartscock's answer, I found out that the like clause uses a sort of limited regex pattern.
For the OP's scenario, MSMS has the solution.
Assuming I want databases ABCOne, ABCTwo, and ABCThree, I come up with what is essentially independent whitelists for each character:
Which is easily extensible to any set of strings. It won't be ironclad, that last pattern would also match ABCOwe, ABCTnr, or ABCOneHippotamus, but if you're filtering a limited set of possible values there's a good chance you can make it work.
You could alternatively use the
[^]
operator to present a blacklist of unacceptable characters.