Solved

need to split

Posted on 2013-11-07
3
322 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
  • 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 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now