?
Solved

need to split

Posted on 2013-11-07
3
Medium Priority
?
333 Views
Last Modified: 2013-11-08
I have column of varchar(5000) which stores the data in below format

;AAA; ;BBBBBBB; ;CCCCCC;

Now i want to split the data and put into different columns in different table.
col 1 col2 col3
AAA  BBBBBBB CCCCCC
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 39631154
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 39631514
If you want dynamic split check this link (try the solution in the end, on dynamic shredding):

http://gallery.technet.microsoft.com/scriptcenter/T-SQL-Script-to-Split-a-308206f3

hth.
0
 
LVL 9

Accepted Solution

by:
Valliappan AN earned 2000 total points
ID: 39632070
Hi,

You may split the string, as given in this link -  on dynamic shredding at the bottom of the page, and then dynamically create the columns.

http://gallery.technet.microsoft.com/scriptcenter/T-SQL-Script-to-Split-a-308206f3

Using the above link code reference, have created a sample script to do the same for e.g.

--Using XML method  

--DROP TABLE ##FormSplitXML
 
;With FormSplitXML (id, name, X)
AS 
( 
  SELECT 10 AS Id, 'test' AS Name, 
    CONVERT(XML,'<r><n>'   
    + REPLACE('AAAAA;BB;CCCC',';', '</n><n>') + '</n></r>') AS X 
) 
 
SELECT * INTO ##FormSplitXML FROM FormSplitXML 
 
 
---Using XML method -  Manual shredding 
 
--SELECT  Id,  
-- i.value('n[1]','varchar(100)') AS Name, 
-- i.value('n[2]','varchar(100)') AS Surname 
-- FROM ##FormSplitXML Spt 
-- CROSS APPLY Spt.X.nodes('/r') x(i) 
 
--Using XML method - Dynamic shredding 
 
SELECT Id, 
       i.value('local-name(.)','varchar(100)') ColumnName, 
       i.value('.','varchar(100)') ColumnValue 
       INTO ##temp 
FROM ##FormSplitXML Spt 
CROSS APPLY Spt.X.nodes('//*[text()]') x(i) 
 
 
DECLARE @SQL NVARCHAR(MAX), 
        @i INT, 
        @MaxCount INT 
 
SELECT @MaxCount = MAX(cnt) 
FROM ( 
    SELECT Id 
        ,COUNT(ColumnValue) AS cnt 
    FROM ##temp 
    GROUP BY Id 
    ) X; 
  
SET @i = 0; 
  
WHILE @i < @MaxCount 
BEGIN 
    SET @i = @i + 1; 
    SET @SQL = COALESCE(@Sql + ', ', '') + 'Name' + cast(@i AS NVARCHAR(10)); 
END 
  
SET @SQL = N';WITH CTE AS ( 
   SELECT Id, ColumnValue, ''Name''  
   + CAST(row_number() OVER (PARTITION BY ID ORDER BY Id DESC) AS Varchar(10)) AS RowNo 
   FROM   ##temp) 
SELECT * 
FROM   CTE 
PIVOT (MAX(ColumnValue) FOR RowNo IN (' + @SQL + N')) pvt'; 
  
--PRINT @SQL; 
  
EXECUTE (@SQL); 
 
DROP TABLE ##temp 
DROP TABLE ##FormSplitXML

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question