Daybreaker
Daybreaker

Reputation: 1047

Split string by two delimiters into two columns

I have a string value which has numeric values separated by comma and then by a pipe. I want to split them into a table with two columns. I could split the string by one delimiter but unfortunately couldn't find a way to split by two. Please help.

DECLARE @list NVARCHAR(MAX) = '1,101|2,202|3,303';

The result should be like below.

1   101
2   202
3   303

Thanks in advance.

Upvotes: 5

Views: 2722

Answers (4)

Ignatius Samuel Megis
Ignatius Samuel Megis

Reputation: 185

Try using this Table-valued Function, embed this SP to your main SP

ALTER FUNCTION [dbo].[delimiter]
(
@PARAM_IDS AS VARCHAR(MAX)
@PARAM_DELIMITER AS CHAR(1)
)
RETURNS 
@NEW_TABLE TABLE 
(
NUM INT NOT NULL IDENTITY,
ID INT NOT NULL
)
AS
BEGIN
DECLARE @NEXTSTRING AS NVARCHAR(MAX);
DECLARE @POS AS INT;
DECLARE @STRING AS NVARCHAR(MAX);
DECLARE @DELIMITER AS NVARCHAR(MAX);
SET @STRING = @PARAM_IDS;
SET @DELIMITER = @PARAM_DELIMITER;
SET @STRING = @STRING + @DELIMITER;
SET @POS = CHARINDEX(@DELIMITER,@STRING);

WHILE (@POS <> 0)
BEGIN
    SET @NEXTSTRING = SUBSTRING(@STRING,1,@POS - 1);
    INSERT @NEW_TABLE (ID) VALUES (@NEXTSTRING);
    SET @STRING = SUBSTRING(@STRING,@POS+1,len(@STRING));
    SET @POS = CHARINDEX(@DELIMITER,@STRING);
END 
    RETURN
END

then example of use

SET @DETAILS_COUNT = (SELECT COUNT(*) FROM delimiter(@PARAM_MS_UNIT_ID, @DELIMITER));

Upvotes: 0

Kapil
Kapil

Reputation: 987

Use xml path and cross apply to create multiple rows for a single row based on the pipe separator and then use substring w.r.t the commas to derive two desired columns

Create table #temp(list nvarchar(max))
Insert into #temp values('1,101|2,202|3,303')
SELECT 
  Substring(Tbl.Col.value('./text()[1]','varchar(50)'),1,1)as col1,
  Substring(Tbl.Col.value('./text()[1]','varchar(50)'),charindex(',',Tbl.Col.value('./text()[1]','varchar(50)'),1)+1,len(Tbl.Col.value('./text()[1]','varchar(50)')))
FROM 
(Select cast('<a>'+ replace((SELECT list As [*] FOR XML PATH  ('')), '|', '</a><a>') + '</a>' as xml)as t 
  from #temp) tl
Cross apply 
tl.t.nodes('/a') AS Tbl(Col)

Upvotes: 0

Jason A. Long
Jason A. Long

Reputation: 4442

If you're using SQL Server 2016 or Azure, you have access to the new SPLIT_STRING function. If not I recommend using Jeff Moden's DelimitedSplit8K function, which is widely regarded as the fastest, most efficient SQL based string splitter available...

DECLARE @list NVARCHAR(MAX) = '1,101|2,202|3,303';

SELECT 
    Col1 = LEFT(dsk.Item, sl.SplitLocation - 1),
    Col2 = SUBSTRING(dsk.Item, sl.SplitLocation + 1, LEN(dsk.Item))
FROM 
    dbo.DelimitedSplit8K(@list, '|') dsk    -- code for DelimitedSplit8K can be found here... http://www.sqlservercentral.com/articles/Tally+Table/72993/
    CROSS APPLY ( VALUES (ISNULL(NULLIF(CHARINDEX(',', dsk.Item, 1), 0), 1)) ) sl (SplitLocation);

Upvotes: 1

Rahul Patel
Rahul Patel

Reputation: 500

CREATE  FUNCTION [dbo].[fn_Split_char](@text nvarchar(max), @delimiter varchar(20) = ' ')
RETURNS @Strings TABLE
(   
  position int IDENTITY PRIMARY KEY,
  value nvarchar(max)  
)
AS
BEGIN

DECLARE @index int
SET @index = -1

WHILE (LEN(@text) > 0)
  BEGIN 
    SET @index = CHARINDEX(@delimiter , @text) 
    IF (@index = 0) AND (LEN(@text) > 0) 
      BEGIN  
        INSERT INTO @Strings VALUES (@text)
          BREAK 
      END 
    IF (@index > 1) 
      BEGIN  
        INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))  
        SET @text = RIGHT(@text, (LEN(@text) - @index)) 
      END 
    ELSE
      SET @text = RIGHT(@text, (LEN(@text) - @index))
    END
  RETURN
END



Select LEFT(value, Charindex(',', value) - 1) ,
RIGHT(value, Charindex(',', Reverse(value)) - 1) ,
* from [fn_Split_char] ('1,101|2,202|3,303', '|')

Upvotes: 0

Related Questions