How does this query work to split csv into rows in sql server? -
How does this query work to split csv into rows in sql server? -
i using sql-server 2012
here query
drop table #t create table #t(id int,name varchar(10)) insert #t values(1,'a,b,c'),(2,'d,e') select a.id,split.a.value('.','varchar(10)') string (select id,cast('<m>'+replace([name],',','</m><m>')+'</m>' xml) string #t) cross apply string.nodes('/m') split(a)
i trying logic of query used here,but unable understand how working?
can body guide me in making understand the
logic in cast()
clause , split.a.value()
the cast clause converts string xml fragment consumed later 'nodes' method. if run subquery:
select id,cast('<m>'+replace([name],',','</m><m>')+'</m>' xml) string #t
you results below:
id string 1 <m>a</m><m>b</m><m>c</m> 2 <m>d</m><m>e</m>
the node method splits xml info relational form. oversimplify, nodes method gives table 'split' column 'a'. value method converts value in node varchar projection: (split.a.value('.','varchar(10)') can interpreted table.column.value(root varchar(10)).
refer next links more:
nodes method: http://msdn.microsoft.com/en-us/library/ms188282.aspx
value method: http://msdn.microsoft.com/en-us/library/ms178030.aspx
sql-server
Comments
Post a Comment