简体   繁体   English

如何在sql server 2005中使用具有多个值的条件?

[英]How to use like condition with multiple values in sql server 2005?

I need to filter out records based on some text matching in nvarchar(1000) column. 我需要根据nvarchar(1000)列中的一些文本匹配过滤出记录。 Table has more than 400 thousands records and growing. 表有超过40万条记录并且还在增长。 For now, I am using Like condition:- 目前,我正在使用Like条件: -

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? 有没有首选的工作?

SELECT 
    *
FROM
    table_01
WHERE
    Text like '%[A-Z]1%'

This will check if the texts contains A1, B1, C1, D1, ... 这将检查文本是否包含A1,B1,C1,D1,......

Reference to using the Like Condition in SQL Server 参考在SQL Server中使用Like条件

You can try the following if you know the exact position of your sub string: 如果您知道子字符串的确切位置,则可以尝试以下操作:

SELECT 
    *
FROM
    table_01
WHERE
    SUBSTRING(Text,1,2) in ('B1','C1','D1')

Have a look at LIKE on msdn . 看看msdn上的LIKE

You could reduce the number filters by combining more details into a single LIKE clause. 您可以通过将更多详细信息组合到单个LIKE子句中来减少数字过滤器。

SELECT 
    *
FROM
    table_01
WHERE
    Text like '%[ABCD]1%'

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. 我需要这样做,以便我可以在SQL Server Profiler跟踪模板中的DatabaseName列的过滤器中允许两个不同的数据库。

All you can do is fill in the body of a Like clause. 您所能做的就是填写Like子句的正文。

Using the reference in John Hartscock's answer , I found out that the like clause uses a sort of limited regex pattern. 使用John Hartscock的答案中的引用,我发现like子句使用了一种有限的正则表达式模式。

For the OP's scenario, MSMS has the solution . 对于OP的情况, MSMS有解决方案

Assuming I want databases ABCOne, ABCTwo, and ABCThree, I come up with what is essentially independent whitelists for each character: 假设我想要数据库ABCOne,ABCTwo和ABCThree,我想出了每个角色基本上是独立的白名单:

Like ABC[OTT][NWH][EOR]%

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. 它不会是铁定的,最后的模式也会与ABCOwe,ABCTnr或ABCOneHippotamus相匹配,但是如果你过滤了一组有限的可能值,那么你很有可能让它成功。

You could alternatively use the [^] operator to present a blacklist of unacceptable characters. 您也可以使用[^]运算符来显示不可接受的字符的黑名单。

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. 如果你可以在表的那一列上创建一个FULLTEXT INDEX (它假定对性能和空间进行了大量的研究),那么你可能会看到文本匹配的性能有了很大的提高。 You can go to this link to see what FULLTEXT SEARCH is and this link to see how to create a FULLTEXT INDEX . 您可以转到此链接查看FULLTEXT SEARCH是什么,以及此链接以了解如何创建FULLTEXT INDEX

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

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