Solved

need to split

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

808 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