问题描述
论坛上的第一篇文章,因为我真的被这个帖子困住了.
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) 格式存储
- 如果您的文本
- 如果编码不是 Unicode 并且是隐式的,由于源代码页的原因,数据库中的字符串代码页应该与您要加载的代码点相同或兼容.如果需要,请使用 COLLATE.如果不存在这样的服务器代码页,则必须添加具有正确编码的显式
- 要使用显式编码,请使用与代码页没有交互的 varbinary() 类型,或者使用适当代码页的字符串类型.然后,将数据分配给
- If your text
- If the encoding is not Unicode and is implicit, because of the source code page, the string code page in the database should be the same as or compatible with the code points that you want to load. If required, use COLLATE. If no such server code page exists, you have to add an explicit
- To use an explicit encoding, use either the varbinary() type, which has no interaction with code pages, or use a string type of the appropriate code page. Then, assign the data to an
示例:显式指定编码
假设您有一个 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: