Wednesday, July 23, 2014

Split the string using the XML data type in the SQL server

Introduction

In this article we going to discuss about how to split the string and convert in to the XML format and from that insert in to on temp table

Create one sample input variable

DECLARE @sampleText varchar(500)='lokesh1,lokesh2,lokesh3,lokesh4,lokesh5,lokesh6,lokesh7,lokesh8,lokesh9,lokesh10'

And create one sample variable with XML data type like  
In this XML data type I assumed <lok></lok> as  the tag notation
Declare @XML XML
set @XML=CAST('<lok>'+REPLACE(@sampleText,',','</lok><lok>')+'</lok>' as XML)

And create one temp table like

DECLARE @temp AS TABLE 
( 
      rownum INT IDENTITY(1, 1), 
      name   VARCHAR(50) 
)

Read More »