1

I just want to split a string as column and rows in MS SQL

Example:

String : 26412|7|554.40#42470|10|1504.40#49606|10|274.40#378982|10|425.84

Delimiter : | as column,

Delimiter : # as rows

help to find solution Thanks in advance.

Expected Output:

Col1    Col2    Col3
-----------------------
26412    07    554.40
42470    10    1504.40
49606    10    274.40
378982   10    425.84
2
  • What version of SQL Server (2016 adds a string split function...) Commented Jul 19, 2016 at 9:39
  • 1
    Can you show us what you've done so far? This is a great place to start: How to post a T-SQL question on a public forum Commented Jul 19, 2016 at 9:42

4 Answers 4

3

This could be done with the help of XML:

DECLARE @str nvarchar(max) = N'26412|7|554.40#42470|10|1504.40#49606|10|274.40#378982|10|425.84',
        @xml xml

SELECT @xml = CAST('<s><b>'+REPLACE(REPLACE(@str,'#','</b></s><s><b>'),'|','</b><b>')+'</b></s>' as xml)


SELECT  t.v.value('b[1]','int') as Col1,
        t.v.value('b[2]','int') as Col2,
        t.v.value('b[3]','float') as Col3
FROM @xml.nodes('/s') as t(v)

Output:

Col1    Col2    Col3
26412   7       554,4
42470   10      1504,4
49606   10      274,4
378982  10      425,84
Sign up to request clarification or add additional context in comments.

2 Comments

@gofr1:I like this approach,do you have any links for further examples
@TheGameiswar I learn it from my coworker, and then find out few answers with that approach on SO. Use this pretty often when I need string splitting. Here is a good place to start.
0

Using one of the split strings from here:

declare @str varchar(max)
set @str='26412|7|554.40#42470|10|1504.40#49606|10|274.40#378982|10|425.84'

;with cte(mainitem)
as
(
select * from [dbo].[SplitStrings_Numbers](@str,'#') b 
)
,cte1 as 
(select *,row_number() over (partition by mainitem order by mainitem) as rn from cte c
cross apply
[dbo].[SplitStrings_Numbers](c.mainitem,'|')  )
select 
max(case when rn=1 then cast(item as varchar(100)) end) as col1,
max(case when rn=2 then cast(item as varchar(100))end) as col2,
max(case when rn=3 then cast(item as varchar(100)) end) as col3
from cte1 
group by mainitem

Output:

26412   7   554.40
378982  10  425.84
42470   10  1504.40
49606   10  274.40

1 Comment

@Kannappan:please see the article i referrred
0

Try like this,

DECLARE @List NVARCHAR(max) = '26412|7|554.40#42470|10|1504.40#49606|10|274.40#378982|10|425.84'

SELECT col1 AS ActualData
    ,PARSENAME(REPLACE(col1, '|', '.'), 4) col1
    ,PARSENAME(REPLACE(col1, '|', '.'), 3) col2
    ,substring(col1, (LEN(col1) - CHARINDEX('|', REVERSE(col1))) + 2, len(col1)) AS Col3
FROM (
    SELECT y.i.value('(./text())[1]', 'nvarchar(4000)') AS col1
    FROM (
        SELECT x = CONVERT(XML, '<i>' + REPLACE(@List, '#', '</i><i>') + '</i>').query('.')
        ) AS a
    CROSS APPLY x.nodes('i') AS y(i)
    ) T

Comments

0

This is working very well for me in MySql 5.5

 CREATE TABLE split ( id INT, str VARCHAR(50) );
 INSERT INTO split VALUES (1, 'Smith'), (2, 'Julio|Jones|Falcons'), (3,
 'White|Snow'), (4, 'Paint|It|Red'), (5, 'Green|Lantern'), (6, 'Brown|bag');


        DELIMITER |

        CREATE PROCEDURE SplitColumnsToRows ()
        BEGIN
          DECLARE id1 INT (100);
          DECLARE str1 VARCHAR (100);
          DECLARE done INTEGER DEFAULT 0;
          DECLARE tokens INT DEFAULT 0;
          DECLARE i INT DEFAULT 0;
          DECLARE token VARCHAR (100);
          DECLARE cur CURSOR FOR
          SELECT
            id,
            str
          FROM
            split;
          DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
          OPEN cur;
          parse :
          LOOP
            FETCH cur INTO id1,
            str1;
            IF done = 1
            THEN LEAVE parse;
            END IF;
            SET tokens =
            (SELECT
              LENGTH(str1) - LENGTH(REPLACE(str1, '|', '')) + 1);
            DELETE
            FROM
              split
            WHERE id = id1
              AND str = str1;
            SET i = 1;
            WHILE
              i <= tokens DO IF i = tokens
              THEN SET token = str1;
              ELSE SET token = SUBSTR(str1, 1, INSTR(str1, '|') - 1);
              SET str1 = SUBSTR(str1, INSTR(str1, '|') + 1, LENGTH(str1));
              END IF;
              INSERT INTO split
              VALUES
                (id1, token);
              SET i = i + 1;
            END WHILE;
          END LOOP parse;
          CLOSE cur;
        END |

        DELIMITER;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.