STUFF AND FOR XML PATH for String Concatenation in SqlServer 末蓝、 2024-02-18 20:28 3阅读 0赞 Today ,we should concatenate column data into single row. To make you understand better,we learn about “STUFF AND FOR XML PATH for String Concatenation in SqlServer”with demo. Let’s learn “SQL Server STUFF() Function” first. <table> <tbody> <tr> <td>STUFF:</td> </tr> </tbody> </table> <table> <tbody> <tr> <td>Example one——Delete 3 characters from a string and then insert another sequence of characters, starting in position 1:</td> </tr> </tbody> </table> SELECT STUFF('SQL Tutorial', 1, 3, 'HTML'); when we run the Query. We will get “HTML Tutorial” like the image. ![这里写图片描述][70] 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. <table> <tbody> <tr> <td>Syntax:</td> </tr> </tbody> </table> STUFF(string1, start, length, add_string) <table> <thead> <tr> <th>Parameter</th> <th align="center">Description</th> </tr> </thead> <tbody> <tr> <td>string1</td> <td align="center">Required. The source string to modify</td> </tr> <tr> <td>start</td> <td align="center">Required. The position in string1 to start delete length characters, and then insert add_string</td> </tr> <tr> <td>length</td> <td align="center">Required. The number of characters to delete from string1</td> </tr> <tr> <td>add_string</td> <td align="center">Required. The sequence of characters to insert into string1 at the start position</td> </tr> </tbody> </table> <table> <tbody> <tr> <td>STUFF AND FOR XML PATH for String Concatenation:</td> </tr> </tbody> </table> 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: ![这里写图片描述][70 1] select distinct 部门 , STUFF((Select ','+姓名 from Employees T1 where T1.部门=T2.部门 FOR XML PATH('')),1,1,'') 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 ! [70]: https://image.dandelioncloud.cn/pgy_files/images/2024/01/29/38821b33db7546c399ecc3bc6b2ede57.png [70 1]: https://image.dandelioncloud.cn/pgy_files/images/2024/01/29/7cad06d0246e460e88826b045acc385e.png
相关 STUFF AND FOR XML PATH for String Concatenation in SqlServer Today ,we should concatenate column data into single row. To make you understand bet 末蓝、/ 2024年02月18日 20:28/ 0 赞/ 4 阅读
相关 SQL Server 使用STUFF()函数和FOR XML PATH()函数实现将字段拼接成字符串 1 使用详解 1.1 STUFF() 说明:拼接字符串,同时可以去掉指定位置指定长度的字符串。 语法: STUFF(character\_expression 超、凢脫俗/ 2023年10月05日 09:39/ 0 赞/ 39 阅读
相关 SQL之 Stuff和For xml path 示例 昨天遇到一个`SQL Server`的问题:需要写一个储存过程来处理几个表中的数据,最后问题出在我想将一个表的一个列的多行内容拼接成一行,比如表中有两列数据 : 叁歲伎倆/ 2021年06月26日 16:06/ 0 赞/ 496 阅读
还没有评论,来说两句吧...