Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>see <a href="https://stackoverflow.com/questions/1456192/comparing-a-column-to-a-list-of-values-in-t-sql/1456404#1456404">my previous answer to this</a></p> <p>this is the best source:</p> <p><a href="http://www.sommarskog.se/arrays-in-sql.html" rel="nofollow noreferrer">http://www.sommarskog.se/arrays-in-sql.html</a></p> <p>create a split function, and use it like:</p> <pre><code>SELECT * FROM YourTable y INNER JOIN dbo.splitFunction(@Parameter) s ON y.ID=s.Value </code></pre> <p><a href="http://www.sommarskog.se/arrays-in-sql-2005.html#tblnum" rel="nofollow noreferrer">I prefer the number table approach</a></p> <p>For this method to work, you need to do this one time table setup:</p> <pre><code>SELECT TOP 10000 IDENTITY(int,1,1) AS Number INTO Numbers FROM sys.objects s1 CROSS JOIN sys.objects s2 ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number) </code></pre> <p>Once the Numbers table is set up, create this function:</p> <pre><code>CREATE FUNCTION [dbo].[FN_ListToTable] ( @SplitOn char(1) --REQUIRED, the character to split the @List string on ,@List varchar(8000)--REQUIRED, the list to split apart ) RETURNS TABLE AS RETURN ( ---------------- --SINGLE QUERY-- --this will not return empty rows ---------------- SELECT ListValue FROM (SELECT LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue FROM ( SELECT @SplitOn + @List + @SplitOn AS List2 ) AS dt INNER JOIN Numbers n ON n.Number &lt; LEN(dt.List2) WHERE SUBSTRING(List2, number, 1) = @SplitOn ) dt2 WHERE ListValue IS NOT NULL AND ListValue!='' ); GO </code></pre> <p>You can now easily split a CSV string into a table and join on it:</p> <pre><code>select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,') </code></pre> <p>OUTPUT:</p> <pre><code>ListValue ----------------------- 1 2 3 4 5 6777 (6 row(s) affected) </code></pre> <p>To make what you need work, use CROSS APPLY:</p> <pre><code>DECLARE @tbl_A table (RowID int, RowValue varchar(500)) DECLARE @tbl_b table (RowID int identity, RowValue varchar(500)) INSERT INTO @tbl_A VALUES (1, 'Java, ASP.Net, C#') INSERT INTO @tbl_A VALUES (2, 'Java') INSERT INTO @tbl_A VALUES (3, 'Java, C#') INSERT INTO @tbl_A VALUES (4, 'html') INSERT INTO @tbl_b (RowValue) SELECT DISTINCT st.ListValue FROM @tbl_A CROSS APPLY dbo.FN_ListToTable(',',RowValue) AS st ORDER BY st.ListValue SELECT * FROM @tbl_b ORDER BY RowID </code></pre> <p>OUTPUT:</p> <pre><code>RowID RowValue ----------- -------------- 1 ASP.Net 2 C# 3 html 4 Java (4 row(s) affected) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload