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

后端 未结 2 911
清酒与你
清酒与你 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: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
    

提交回复
热议问题