Search code examples
sql-serverxmlsql-server-2014varcharvarbinary

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


i try to convert varbinary to varchar

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

result:

ï>>¿<?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))
....

Why i get characters "ï>>¿" ?


Solution

  • That's the UTF-8 Byte Order Mark, where the bytes 0xEFBBBF are prepended to a file to indicate the encoding. SQL Server is not introducing these bytes; they are present in your data. To remove them, you can conver the column to XML, and the XML parser will ignore them. EG:

    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)
    

    outputs

    <?xml version="1.0" encoding="utf-8" standalone="no"?><foo/>   <foo />
    
    (1 row affected)