How to SELECT using both wildcards (LIKE) and array (IN)?

后端 未结 2 907
清酒与你
清酒与你 2021-02-19 07:08

In SQL, if you want to perform a SELECT with a wildcard, you\'d use:

SELECT * FROM table_name WHERE field_name LIKE \'%value%\'

If you wanted t

相关标签:
2条回答
  • 2021-02-19 07:35
    SELECT *
    FROM table_name
    WHERE field_name LIKE '%one'
       OR field_name LIKE '_two'
       OR field_name LIKE 'three[abv]'
    
    0 讨论(0)
  • 2021-02-19 07:43

    you can use join with like statement like below query:

    SELECT * FROM table_name t 
    JOIN dbo.Split('one,two,three',',') s ON t.field_name LIKE N'%'+s.item+'%'
    

    I create this function to split string:

    CREATE FUNCTION [dbo].[Split] (@StringToSplit NVARCHAR(MAX), @SpliterChar CHAR(1))
    RETURNS
        @returnList TABLE ([item] [NVARCHAR] (500))
    AS
    BEGIN
     DECLARE @nItem NVARCHAR(500);
     DECLARE @pos INT; 
     WHILE CHARINDEX(@SpliterChar, @StringToSplit) > 0
     BEGIN
      SELECT @pos  = CHARINDEX(@SpliterChar, @StringToSplit); 
      SELECT @nItem = SUBSTRING(@StringToSplit, 1, @pos-1);
      if (@nItem <> '') INSERT INTO @returnList SELECT @nItem;
      SELECT @StringToSplit = SUBSTRING(@StringToSplit, @pos+1, LEN(@StringToSplit)-@pos);
     END
     if (@StringToSplit<>'') INSERT INTO @returnList SELECT @StringToSplit;
     RETURN
    END
    
    0 讨论(0)
提交回复
热议问题