Sql In condition with comma separator

Mohammad Farook 161 Reputation points
2021-08-23T08:27:43.517+00:00

Hi,
create table #temp (ID INT)
INSERT INTO #temp(ID) VALUES (1)

create table #tempTable (ID INT,Menus VARCHAR(10))
INSERT INTO #tempTable(ID,Menus) VALUES (1001,'1'),(1002,'1,2'),(1003,'2,3'),(1004,'1,4'),(1005,'11,4')

select * from #tempTable where (Menus) LIKE (SELECT CONVERT(VARCHAR(20),ID) FROM #temp)

drop table #temp,#tempTable

I need result like
1001
1002
1004

Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

Accepted answer
  1. Vaibhav Chaudhari 38,916 Reputation points Volunteer Moderator
    2021-08-23T08:42:39.32+00:00

    Does this work?

    SELECT ID
    FROM (
        SELECT ID
            ,Menus
            ,value
        FROM #tempTable
        CROSS APPLY STRING_SPLIT(Menus, ',')
        ) T
    WHERE value = 1
    

    Please don't forget to Accept Answer and Up-vote if the response helped -- Vaibhav

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. MelissaMa-MSFT 24,221 Reputation points
    2021-08-23T08:48:59.527+00:00

    Hi @Mohammad Farook ,

    Welcome to Microsoft Q&A!

    What is the version of your SQL Server?

    If it is SQL Server 2016 and later, please refer below:

     SELECT id   
     FROM #tempTable    
     CROSS APPLY STRING_SPLIT(Menus, ',')  
     WHERE VALUE IN (SELECT ID FROM #temp)  
    

    Output:

    ID  
    1001  
    1002  
    1004  
    

    If it is SQL Server 2014 and earlier, please refer below:

    Create one function, refer to this forum.

    CREATE FUNCTION [dbo].[SplitString]  
    (  
        @List NVARCHAR(MAX),  
        @Delim VARCHAR(255)  
    )  
    RETURNS TABLE  
    AS  
        RETURN ( SELECT [Value] FROM   
          (   
            SELECT   
              [Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],  
              CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))  
            FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)  
              FROM sys.all_objects) AS x  
              WHERE Number <= LEN(@List)  
              AND SUBSTRING(@Delim + @List, [Number], LEN(@Delim)) = @Delim  
          ) AS y  
        );  
    

    Then call this function as below:

     SELECT id  
     FROM #tempTable    
     CROSS APPLY dbo.SplitString(Menus, ',')  
     WHERE VALUE IN (SELECT ID FROM #temp)  
    

    Best regards,
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-08-23T21:11:04.86+00:00

    Storing comma-separated lists in table columns is very rarely the right thing to do.

    Relational databases are designed from the idea of one value per cell. If you work against that principle, you will be duly punished by having to write complex queries with poor performance.

    So do as Viorel suggest and redesign the table.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.