Solved

need to split

Posted on 2013-11-07
3
310 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

758 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

22 Experts available now in Live!

Get 1:1 Help Now