Click here to Skip to main content
15,906,296 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
SELECT Stuff(
  (SELECT N', ' + Name FROM Names FOR XML PATH(''),TYPE)
  .value('text()[1]','nvarchar(max)'),1,2,N'')


stuff is used for replacing by removing a substring
and for xml path is used for getting xml data in a single row but multiple nodes

but i m unable to understand what going on in this
" .value('text()[1]','nvarchar(max)'),1,2,N'') "

my guess is it somehow replaces xml nodes to get comma separated single row data.
but i am confused and i want to understand it all.
please gimme a full explation if its possible.
thank you so much .
Posted

1 solution

your understanding is correct. this is to generate Comma separate value for a list of time.

When FOR XML is used this mean generate the related data in XML.

PATH is a simpler way to define XML element and attributes.

for details please refer FOR XML Path(SQL Server)

So when we use use string concatenation

SELECT N', ' + Name , this means FOR XML will roll up all the values and consider it a single node.

Following code
".value('text()[1]','nvarchar(max)')"

will considered the node value which text()[1] of data type nvarchar(max). you can also use nvarchar(4000).

Consider the following example:

SQL
select 
(Select ', ' + TABLE_NAME
from INFORMATION_SCHEMA.TABLES
FOR XML PATH(''),TYPE).value('text()[1]','nvarchar(4000)')

Hope it help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900