How to make FOR XML PATH not choke on ASCII Control Codes

牧云@^-^@ 提交于 2020-01-06 15:16:34

问题


If I execute the code below in SQL Server 2005:

stuff((
    SELECT char(31)
        + RTRIM(LTRIM(RTRIM(LTRIM(RTRIM(LTRIM(RTRIM(LTRIM(
            u.GivenName)) + ' ' + LTRIM(
            u.MiddleName))) + ' ' + LTRIM(
            u.FirmSurName))) + ' ' + LTRIM(
            u.NameTitle)))
    FROM  my_database.my_schema.my_table u
    FOR XML PATH(''),TYPE).value('.','nvarchar(max)')
    ,1,1,'') 

I get this error:

FOR XML could not serialize the data for node 'NoName' because it contains a character (0x001F) which is not allowed in XML. To retrieve this data using FOR XML, convert it to binary, varbinary or image data type and use the BINARY BASE64 directive.

The issue is the number passed to char, above. Similar errors occur as you decrement the number, from 30 on down to 20 (these are all control codes, rather than visible characters). However, 20 through 31 are perfectly valid XML characters; reference here: http://www.w3.org/TR/2008/REC-xml-20081126/#charsets

Note that the statement works completely as intended for char(32), char(9), or any other printable ASCII character I've yet tried it on.

My question is, how can I maintain the above functionality while also allowing those other 12 characters? I'm particularly interested in 31, 30, 29, and 28, as they seem explicitly intended for separating structured data, which is what I'm doing here, but more broadly, I'd like access to the full set of XML characters.

EDIT: Ooops, misread the spec! Answered by Mikael.

来源:https://stackoverflow.com/questions/21049847/how-to-make-for-xml-path-not-choke-on-ascii-control-codes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!