STUFF AND FOR XML PATH for String Concatenation in SqlServer
Today,we should concatenate column data into single row.
SELECT STUFF('SQL Tutorial',1,3,'HTML');
when we run the Query. We will get “HTML Tutorial” like the image. After learning the demo,we can learn about the Definition and Usage The STUFF() function deletes a sequence of characters from a string and then inserts another sequence of characters into the string,starting at a specified position.
STUFF(string1,start,length,add_string)
OK,after learning about Stuff function(),we can learn ‘STUFF AND FOR XML PATH for String Concatenation in SqlServer’ In the work,we should concatenate all the name data in a department into single row. Like the Image: select distinct 部门,STUFF((Select ','+姓名 from Employees T1 where T1.部门=T2.部门 FOR XML PATH('')),'') as 同一个部门的人员名单 from Employees T2
We can use XmlPath(”) to concatenate column data into single row. Stuff is used to remove the first ‘,’ after string concatenation. If you have discovered something I should add,let me know.thanks you very much ! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |