Home > database >  How to do a Regex Pattern in SQL Server for specific combination
How to do a Regex Pattern in SQL Server for specific combination

Time:11-06

I am trying to get codes that have certain combination of characters in a query in sql server.

Instead of saying in SQL Server

and TIL.code LIKE '%tf%' or code like 'fs%' or code like '%ls%' 

Is there a single pattern I can use

CodePudding user response:

If your goal is to avoid the ORs, rather than use Regex (which SQL Server doesn't support) you could use an EXISTS:

AND EXISTS (SELECT 1
            FROM (VALUES('%tf'),('fs%'),('%ls%'))V(Pattern)
            WHERE TIL.Code LIKE V.Pattern)

If this is coming from a parameter, you would use a table type parameter, and then replace the VALUES table construct with that parameter:

AND EXISTS (SELECT 1
            FROM @YourTableTypeParameter TTP
            WHERE TIL.Code LIKE TTP.YourPatternColumn)
  • Related