devxlogo

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

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

Use this code:

 if object_id('tblTemp')<>0 drop table tblTempCreate Table tblTemp (Field1 varchar(5),Field2 Varchar(100))insert into tblTempselect 'C1' Field1, 'Value1,Value2' Field2Unionselect 'C2' Field1, 'Value3,Value4' Field2 if object_id('tblTempOutPut')<>0 drop table tblTempOutPutselect * into tblTempOutPut from tblTemp where 1=2 set nocount onDECLARE tables_cursor CURSORFORSelect 'insert into tblTempOutPut Select ''' +  Field1 + ''' Field1 , ''' _+ replace(Field2 , ',' ,''' Field2 Union Select ''' + Field1 + ''' Field1, ''') _+ ''' Field2' from tblTempOPEN tables_cursorDECLARE @SQLLine varchar(1000)FETCH NEXT FROM tables_cursor INTO @SQLLineWHILE (@@FETCH_STATUS <> -1)BEGIN   EXEC (@SQLLine)   FETCH NEXT FROM tables_cursor INTO @SQLLineENDDEALLOCATE tables_cursorset nocount offselect * from tblTempOutPut
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist