SQL Server 2008 XML路径分隔列表,使用换行符代替分号



我有一个SSRS报告,它使用以下查询,其中列出了逗号分隔的名称:

Select distinct 
    ST2.Country, st2.Location, st2.[Group], 
    substring(
    (
            Select ';'+ST1.[Sponsor]  AS [text()]
            From tbl_de_list ST1
            Where ST1.[W ID] = ST2.[W ID]            
            For XML PATH ('')
     ), 2, 1000) [sponsor]
From 
    tbl_de_list ST2
order by 
    st2.[W ID]

我需要这是从ssrs报告导出时在一个新的行。

这是我的:

first name, last name ; first name, last name

我需要的(单列):

first name, last name
first name, last name

在Google上做了一些研究之后,我调整了我的查询,添加了一个空格而不是分号作为分隔符。

DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10)
Select distinct ST2.Country, st2.Location,st2.[Group], 
substring(
    (
        Select ';'+ST1.[Sponsor]  AS [text()]
        From tbl_de_list ST1
        Where ST1.[W ID] = ST2.[W ID]            
        For XML PATH ('')
    ), 2, 1000),';',@NewLineChar) [Sponsor]
From tbl_de_list ST2
order by st2.[W ID]

当您将报告导出为excel/csv时,它被推到新行。

最新更新