varbinary to string on SQL Server

后端 未结 7 1075
时光说笑
时光说笑 2020-11-27 15:41

How to convert a column value from varbinary(max) to varchar in human-readable form?

相关标签:
7条回答
  • 2020-11-27 16:14

    The following expression worked for me:

    SELECT CONVERT(VARCHAR(1000), varbinary_value, 2);
    

    Here are more details on the choice of style (the third parameter).

    0 讨论(0)
  • 2020-11-27 16:17

    Actually the best answer is

    SELECT CONVERT(VARCHAR(1000), varbinary_value, 1);
    

    using "2" cuts off the "0x" at the start of the varbinary.

    0 讨论(0)
  • 2020-11-27 16:17

    I tried this, it worked for me:

    declare @b2 VARBINARY(MAX) 
    set @b2 = 0x54006800690073002000690073002000610020007400650073007400
    SELECT CONVERT(nVARCHAR(1000), @b2, 0);
    
    0 讨论(0)
  • 2020-11-27 16:22

    For a VARBINARY(MAX) column, I had to use NVARCHAR(MAX):

    cast(Content as nvarchar(max))
    

    Or

    CONVERT(NVARCHAR(MAX), Content, 0)
    VARCHAR(MAX) didn't show the entire value
    
    0 讨论(0)
  • 2020-11-27 16:27

    Try this

    SELECT CONVERT(varchar(5000), yourvarbincolumn, 0)
    
    0 讨论(0)
  • 2020-11-27 16:40

    "Converting a varbinary to a varchar" can mean different things.

    If the varbinary is the binary representation of a string in SQL Server (for example returned by casting to varbinary directly or from the DecryptByPassPhrase or DECOMPRESS functions) you can just CAST it

    declare @b varbinary(max)
    set @b = 0x5468697320697320612074657374
    
    select cast(@b as varchar(max)) /*Returns "This is a test"*/
    

    This is the equivalent of using CONVERT with a style parameter of 0.

    CONVERT(varchar(max), @b, 0)
    

    Other style parameters are available with CONVERT for different requirements as noted in other answers.

    0 讨论(0)
提交回复
热议问题