Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a fairly fast method to split strings using only T-SQL and you input parameter is only a string. You need to have a table and a function (as described below) already set up to use this method.</p> <p>create this table:</p> <pre><code>CREATE TABLE Numbers (Number int not null primary key identity(1,1)) DECLARE @n int SET @n=1 SET IDENTITY_INSERT Numbers ON WHILE @N&lt;=8000 BEGIN INSERT INTO Numbers (Number) values (@n) SET @n=@n+1 END SET IDENTITY_INSERT Numbers OFF </code></pre> <p>create this function to split the string array (I have other versions, where empty sections are eliminated and ones that do not return row numbers):</p> <pre><code>CREATE FUNCTION [dbo].[FN_ListAllToNumberTable] ( @SplitOn char(1) --REQUIRED, the character to split the @List string on ,@List varchar(8000) --REQUIRED, the list to split apart ) RETURNS @ParsedList table ( RowNumber int --REQUIRED, the list to split apart ,ListValue varchar(500) --OPTIONAL, the character to split the @List string on, defaults to a comma "," ) AS BEGIN --this will return empty rows, and row numbers INSERT INTO @ParsedList (RowNumber,ListValue) SELECT ROW_NUMBER() OVER(ORDER BY number) AS RowNumber ,LTRIM(RTRIM(SUBSTRING(ListValue, number+1, CHARINDEX(@SplitOn, ListValue, number+1)-number - 1))) AS ListValue FROM ( SELECT @SplitOn + @List + @SplitOn AS ListValue ) AS InnerQuery INNER JOIN Numbers n ON n.Number &lt; LEN(InnerQuery.ListValue) WHERE SUBSTRING(ListValue, number, 1) = @SplitOn RETURN END go </code></pre> <p>here is an example of how to split the parameter apart:</p> <pre><code>CREATE PROCEDURE TestPass ( @ArrayOfInts varchar(255) --pipe "|" separated list of IDs ) AS SET NOCOUNT ON DECLARE @TableIDs TABLE (RowNumber int, IDValue int null) INSERT INTO @TableIDs (RowNumber, IDValue) SELECT RowNumber,CASE WHEN LEN(ListValue)&lt;1 then NULL ELSE ListValue END FROM dbo.FN_ListAllToNumberTable('|',@ArrayOfInts) SELECT * FROM @TableIDs go </code></pre> <p>this is based on: <a href="http://www.sommarskog.se/arrays-in-sql.html" rel="nofollow noreferrer">http://www.sommarskog.se/arrays-in-sql.html</a></p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      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