当我将 varbinary 转换为 varchar 时,添加了一些新字符。怎么修?

When i convert varbinary to varchar some new characters added. How to fix?

我尝试将 varbinary 转换为 varchar

declare @binaryData varbinary(max)
set @binaryData = (select "columnName" from "tableName" where ID = 1)
select convert(varchar(max), @binaryData ) as BinaryData

结果:

ï>>¿<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"[]>
....
//(XML File(svg))
....

为什么我得到字符“ï>>¿”?

这是 UTF-8 Byte Order Mark,其中字节 0xEFBBBF 被添加到文件中以指示编码。 SQL服务器没有引入这些字节;它们存在于您的数据中。要删除它们,您可以将该列转换为 XML,XML 解析器将忽略它们。例如:

declare @doc varbinary(max) = 0xEFBBBF + convert(varbinary(2000),'<?xml version="1.0" encoding="utf-8" standalone="no"?><foo/>' )
select cast(@doc as varchar(max)),  cast(@doc as xml)

输出

<?xml version="1.0" encoding="utf-8" standalone="no"?><foo/>   <foo />

(1 row affected)