Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>using a UDF makes the most sense it's flexible for all projects, the one I generally use is at my blog,</p> <p><a href="http://sqlthis.blogspot.com/2005/02/list-to-table.html" rel="nofollow noreferrer">http://sqlthis.blogspot.com/2005/02/list-to-table.html</a></p> <p>because it's written to take your input string and a delimiter, it can be any single character for a delimiter. I wrote the one at the link above, but then afterwards I found many sites publishing a similar solution, so parts may have been inspired from forums that I am a member of...</p> <p>it works, and hopefully it will work for you as well.</p> <p><strong>edit</strong> 2017-08-09 as suggested, I've cloned the code block below. thanks!</p> <pre><code>CREATE FUNCTION udfListToTable (@HList VarChar(2000), @Delimiter CHAR(1)) RETURNS @ListTable TABLE (Field1 VARCHAR(6)) AS BEGIN --By: Francisco Tapia --Date: 2/1/2005 --Purpose: To convert a Comma delimited text to a Temp Variable table To help avoid dynamic sql -- Instead you can join the temp table or use it in your where clause if a field is IN the subquery DECLARE @FieldText as VarChar(6) IF RIGHT(RTRIM(@HLIST),1) &lt;&gt;@Delimiter SET @HList = @HList + @Delimiter WHILE CHARINDEX(@Delimiter, @HList) &gt; 0 BEGIN IF CHARINDEX(@Delimiter, @HList) &gt; 0 BEGIN SELECT @FieldText =LEFT(@HList, CHARINDEX(@Delimiter, @HList)-1) END ELSE BEGIN SELECT @FieldText = RTRIM(LTRIM(@HList)) END --Insert into Variable Table INSERT INTO @ListTable(Field1) SELECT RTRIM(LTRIM(@FieldText)) --Remove Item from list SELECT @HList = RIGHT(RTRIM(@HList), LEN(RTRIM(@HList)) - CHARINDEX(@Delimiter, @HList)) END RETURN END </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. This table or related slice is empty.
    1. This table or related slice is empty.
    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