博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STUFF AND FOR XML PATH for String Concatenation in SqlServer
阅读量:2241 次
发布时间:2019-05-09

本文共 1696 字,大约阅读时间需要 5 分钟。

 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.
 

STUFF:

Example one——Delete 3 characters from a string and then insert another sequence of characters, starting in position 1:
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.

Syntax:
STUFF(string1, start, length, add_string)
Parameter Description
string1 Required. The source string to modify
start Required. The position in string1 to start delete length characters, and then insert add_string
length Required. The number of characters to delete from string1
add_string Required. The sequence of characters to insert into string1 at the start position
STUFF AND FOR XML PATH for String Concatenation:

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('')),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 !

你可能感兴趣的文章
Intellij IDEA使用(六)—— 使用Intellij IDEA创建Java项目并配置jar包
查看>>
Eclipse使用(十)—— 使用Eclipse创建简单的Maven Java项目
查看>>
Eclipse使用(十一)—— 使用Eclipse创建简单的Maven JavaWeb项目
查看>>
Intellij IDEA使用(十三)—— 在Intellij IDEA中配置Maven
查看>>
面试题 —— 关于main方法的十个面试题
查看>>
集成测试(一)—— 使用PHP页面请求Spring项目的Java接口数据
查看>>
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
FastDFS集群架构配置搭建(转载)
查看>>
HTM+CSS实现立方体图片旋转展示效果
查看>>
FFmpeg 命令操作音视频
查看>>
问题:Opencv(3.1.0/3.4)找不到 /opencv2/gpu/gpu.hpp 问题
查看>>
目的:使用CUDA环境变量CUDA_VISIBLE_DEVICES来限定CUDA程序所能使用的GPU设备
查看>>
问题:Mysql中字段类型为text的值, java使用selectByExample查询为null
查看>>
程序员--学习之路--技巧
查看>>
解决问题之 MySQL慢查询日志设置
查看>>
contOS6 部署 lnmp、FTP、composer、ThinkPHP5、docker详细步骤
查看>>
TP5.1模板布局中遇到的坑,配置完不生效解决办法
查看>>
PHPstudy中遇到的坑No input file specified,以及传到linux环境下遇到的坑,模板文件不存在
查看>>
TP5.1事务操作和TP5事务回滚操作多表
查看>>