Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...