Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: T-SQL
Expertise: Beginner
Dec 29, 2003

Get Separate Records for the Comma Delimited Values of One Field (TSQL)

Use this code:
 
if object_id('tblTemp')<>0 drop table tblTemp
Create Table tblTemp (Field1 varchar(5),Field2 Varchar(100))
insert into tblTemp
select 'C1' Field1, 'Value1,Value2' Field2
Union
select 'C2' Field1, 'Value3,Value4' Field2
 
if object_id('tblTempOutPut')<>0 drop table tblTempOutPut
select * into tblTempOutPut from tblTemp where 1=2 
set nocount on
DECLARE tables_cursor CURSOR
FOR
Select 'insert into tblTempOutPut Select ''' +  Field1 + ''' Field1 , ''' _
+ replace(Field2 , ',' ,''' Field2 Union Select ''' + Field1 + ''' Field1, ''') _
+ ''' Field2' from tblTemp
OPEN tables_cursor
DECLARE @SQLLine varchar(1000)
FETCH NEXT FROM tables_cursor INTO @SQLLine
WHILE (@@FETCH_STATUS <> -1)
BEGIN
   EXEC (@SQLLine)
   FETCH NEXT FROM tables_cursor INTO @SQLLine
END
DEALLOCATE tables_cursor
set nocount off
select * from tblTempOutPut
K.Nageswara Rao
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap