SQL“东西”和“FOR XML PATH”生成奇怪的符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16547175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:13:18  来源:igfitidea点击:

SQL 'stuff' and 'FOR XML PATH' generating strange symbols

sqlsql-servertsql

提问by Art F

I have the following query inside a larger Select statement in SQL Server:

我在 SQL Server 中较大的 Select 语句中有以下查询:

 CONVERT(NVARCHAR(2000),stuff((SELECT '; ' + IsNull(D2.SelectedComments,'')
 FROM #StudentDetails D2
 WHERE D2.STUD_PK = A.STUD_PK AND D2.CourseNo = A.CourseNo
AND D2.Section = A.Section
FOR XML PATH('')),1,2,'')) AS SelectedComments,

This column is generating some strange symbols after certain entries such as This approach is satisfactory .&#x0D. I don't understand where the .&#x0Dis coming from. I tried doing SELECT SelectedComments FROM #StudentDetailsright before this and I don't see the .&#x0D. Can anyone tell where it could be coming from?

此列在某些条目后生成一些奇怪的符号,例如This approach is satisfactory .&#x0D. 我不明白这.&#x0D是从哪里来的。SELECT SelectedComments FROM #StudentDetails在此之前我尝试过做,但我没有看到.&#x0D. 谁能告诉它可能来自哪里?

回答by Thomas

If you modify your use of For Xml Path, it will do the unescaping for you and you won't need to resort to using the Replace function:

如果您修改 For Xml Path 的使用,它将为您执行转义操作,您无需求助于使用 Replace 函数:

,   Stuff(
        (
        Select '; ' + IsNull( D2.SelectedComments, '' )
        From #StudentDetails As D2
        Where D2.Stud_PK = A.Stud_PK
            And D2.CourseNo = A.CourseNo
            And D2.Section = A.Section
        For Xml Path(''), type
        ).value('.', 'nvarchar(max)')
        , 1, 2, '') As SelectedComments

回答by M.Ob

The &#x0D is a carriage return. You can either clean up the data before inserting it, remove it, or, if you want to keep the formatting, add TYPE to the end of your SELECT:

是回车。您可以在插入之前清理数据,将其删除,或者,如果您想保留格式,请将 TYPE 添加到 SELECT 的末尾:

SELECT * FROM MyData FOR XML PATH(''), TYPE