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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date