How to use IN with string comma separated



  • I have value in table1 like this:

    Value
    -------
    JT, KF, LF
    

    On select in other table how can I get a value like this

    Select a.Field from table2
    Inner Join Table1 on Table1.field = Table2.field
    Where Type IN(Table2.Value)
    


  • Which version of SQL are you using? If you are in 2016+ then you can use STRING_SPLIT (see example below).

    SELECT a.Field 
    FROM table2
        INNER JOIN Table1 
            ON Table1.field = Table2.field
    WHERE Type IN 
        ( 
        SELECT LTRIM(RTRIM(P.[value])) 
        FROM STRING_SPLIT(Table1.[Value], ',') AS P
        )
    

    Reference: https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-ver15 (Microsoft | SQL Docs)




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2