代码之家  ›  专栏  ›  技术社区  ›  Mathieu Diepman

为什么在将字符串转换为SQLServerXML数据类型时,回车符会消失?

  •  3
  • Mathieu Diepman  · 技术社区  · 15 年前

    每当我将一个字符串解析为SQLXML数据类型时,我注意到只要空格和XML回车混合在一起,回车就会消失。如果它们不是混合的,事情就很好了。有人知道为什么会这样吗?

    请参见下面的SQL示例代码

    DECLARE @var XML
    
    PRINT 'This is a statement with two XML carriage-return characters'
    SET @var = CONVERT (XML,'<root>Dear Sir,&#xD;&#xD;I am passing CR</root>',1)
    SELECT @var
    
    PRINT 'output is identical to a statement with two whitespace carriage-return characters'
    SET @var = CONVERT (XML,'<root>Dear Sir,
    
    I am passing CR</root>',1)
    SELECT @var
    
    PRINT 'Why does this statement only display one space? There is an XML carriage-return AND a whitespace carriage-return character.' 
    
    --Make sure there is no space after &#xD; after you've copied and pasted the code 
    SET @var = CONVERT (XML,'<root>Dear Sir,&#xD;
    I am passing CR</root>',1)
    SELECT @var
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   Andomar    15 年前

    在Windows中,行尾是CR-LF。在Unix中,行尾是CR。

    在第一个示例中,您使用了CR-CR。我猜SQL Server将其解释为Unix样式的行结尾,并给出两个空格。

    第二个例子是在Windows中输入的,给出了CR-LF-CR-LF。这被解释为Windows风格的行尾,给出两个空格。

    你的第三个例子是CR-CR-LF。这显然被解释为Windows风格的行尾,给出一个空格和一个不匹配的CR。

    如果您将第三个示例更改为使用CR-LF,或者 &#xD;&#xA; ,它将显示两个空格。