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

前端 未结 5 1749
醉酒成梦
醉酒成梦 2021-01-11 19:36

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:

5条回答
  •  一向
    一向 (楼主)
    2021-01-11 20:19

    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:

    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.

    You could alternatively use the [^] operator to present a blacklist of unacceptable characters.

提交回复
热议问题