我尝试将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 Server没有引入这些字节;它们存在于您的数据中。要删除它们,可以将列转换为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)