格式化使用For XML Path的STUFF命令的结果,以将信息连接到一个单元格中 [英] Formatting Results of STUFF command that uses For XML Path to concatenate the information into one cell

查看:263
本文介绍了格式化使用For XML Path的STUFF命令的结果,以将信息连接到一个单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个STUFF命令来从列中拉入数据并将它们连接在一起成为一个单元格。

  STUFF((选择CAST(v.ID as VARCHAR)+','

从Source

其中CriteriaIsMet
对于XML Path('')),1,0,'')为MonitorID

我试图完成的是在最后格式化结果。上面代码的结果是这样的:

  12345,23456,34567,456789,

我正在寻找代码来换行。我希望他们都仍然在同一个单元格,而不是分手,但是当放在Excel中,我希望这是结果:

  12345,
23456,
34567,
45678,



  STUFF((选择REPLACE(CAST v.ID as VARCHAR)+',' ',',+ CHAR(13))

然而,最终的结果并不能给我

  123456
 234567&#x0D 

我相信我需要在声明的最后做一些事情,但是,我不确定如何完成这个,我使用的是:

 对于XML路径('')),1,0,'')
code>

我希望有人可以有一个简单的解决方案。我将不胜感激任何帮助。请注意我正在试图保持数据在同一个单元格。这个数据拉的最终结果进入Excel中的数据透视表,我希望数字垂直而不是水平显示。当水平显示时,数字在传递单元格长度后被截断。

解决方案

试试这个...



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $来源
其中CriteriaIsMet
对于XML路径(''),TYPE)
.value('。','NVARCHAR(MAX)'),1,0,'')MonitorID


I am using a STUFF Command to pull in data from a column and concatenate them together into one cell.

STUFF((Select CAST(v.ID as VARCHAR)+ ', '

From Source

Where CriteriaIsMet
For XML Path('')),1,0,'') as MonitorID

What I am trying to accomplish is formatting the results at the end. The results from the code above come out like this:

 12345, 23456, 34567, 456789,

I am looking for the code to have a line break. I want them all to still be in the same cell and not broken up but when placed in Excel I am hoping to have this be the results:

12345,
23456,
34567,
45678,

I have tried using:

 STUFF((Select REPLACE(CAST v.ID as VARCHAR) + ', ' , ', ', +CHAR(13))

However the end results don't give me what I am looking for I end up with this:

123456
234567&#x0D

I believe I need to do something at the end of the statement to make this happen, however, I am not sure how to accomplish this because I am using:

For XML Path('')),1,0,'') 

I am hoping that someone may have a simple solution. I would greatly appreciate any assistance. Please note I am trying to keep the data in the same cell. The end result of this data pull goes into a Pivot table in Excel and i'd like for the numbers to display vertically rather than horizontally. When displayed Horizontally the numbers cut off after passing the length of the cell.

解决方案

Try this...

SELECT STUFF((Select  CAST(v.ID as VARCHAR(30)) +  + ', '+ CHAR(10)
       From Source
       Where CriteriaIsMet
       For XML Path(''),TYPE)
      .value('.','NVARCHAR(MAX)'),1,0,'') as MonitorID

这篇关于格式化使用For XML Path的STUFF命令的结果,以将信息连接到一个单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆