本文介绍了SQL Server 2008R2 和创建

问题描述

论坛上的第一篇文章,因为我真的被这个帖子困住了.

First post on forum since I am really stuck on this one.

以下查询将有效的

The following query correctly assigns a valid

select @xTemp

注意:连接的字符串长度=749,这是正确的.

Note: The length of the concatenated strings = 749, which is correct.

set @xFinal

注意:这个变量的长度回到了711!

Note: The length of this variable is back to 711!

select @xFinal

变量仍然是一个有效的

The variable is still a valid

我做错了什么?

非常感谢所有帮助!

推荐答案

您错过了测试中的一个步骤.试试这个:

You missed a step in your testing. Try this:

SELECT CONVERT(

它将返回一个空单元格.

It will return an empty cell.

根据您在做什么(即最终转换为 VARCHAR),没有理由从 FOR 子句中删除 , TYPE 然后连接 @s

Based on what you are doing (i.e. converting to VARCHAR in the end), there is no reason to start with the , TYPE from the FOR clause and then just concatenate @s

发生这种情况的原因如下:

The reason this is happening is noted here: Limitations of the

将 数据类型实例中时,不会保留 .这是设计使然.) 及其属性(版本/编码/独立)在数据转换为 类型后丢失.

如何正确处理从 (在文本编码"下)

How to properly handle extracting data from an

SQL Server 2005 以 Unicode (UTF-16) 格式存储

示例:显式指定编码
假设您有一个 varbinary(max)
以便保留字节表示,然后最后将其转换为

Example: Explicitly Specifying an Encoding
Assume that you have an varbinary(max) so that the byte representation is preserved, and then finally casts it to

SELECT CAST(
    CAST (('<?

以下 S.O.问题是相关的:


The following S.O. questions are related: