SQL Server converting varbinary to string
Asked Answered
T

6

56

I want to do conversion in T-SQL from a varbinary type to string type

Here is an example :

First I got this varbinary

0x21232F297A57A5A743894A0E4A801FC3

And then I want to convert it to

21232f297a57a5a743894a0e4a801fc3

How to do this?

Teferi answered 27/8, 2012 at 9:8 Comment(3)
What version of SQL Server do you have? Andras's answer only works on 2008 or higher, for example.Ankylose
That's true, I updated my answer, so if you have older version you need a different solutionScrivner
Possible duplicate of varbinary to string on SQL ServerHostetter
S
91

Try:

DECLARE @varbinaryField varbinary(max);
SET @varbinaryField = 0x21232F297A57A5A743894A0E4A801FC3;

SELECT CONVERT(varchar(max),@varbinaryField,2), 
@varbinaryField

UPDATED: For SQL Server 2008

Sixtasixteen answered 27/8, 2012 at 9:12 Comment(3)
It can also be used when retrieving directly from a table: SELECT CONVERT(VARCHAR(MAX), MyBinaryField, 2) FROM MyTable WHERE ID = 123456Modify
If you wish to preserve the '0x' prefix, use the 1 style Flag in the CONVERT expression (instead of 2). SELECT CONVERT( VARCHAR(MAX), MyBinaryField, 1) FROM MyTable WHERE ID = 123456;.Kovar
And how check it for null inline? DECLARE @varbinaryField varbinary(max); SET @varbinaryField = 0x21232F297A57A5A743894A0E4A801FC3; SELECT CONVERT(varchar(max), ISNULL(@varbinaryField, 'value is NULL'), 2), @varbinaryField raise error Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.Theme
O
13

I know this is an old question, but here is an alternative approach that I have found more useful in some situations. I believe the master.dbo.fn_varbintohexstr function has been available in SQL Server at least since SQL2K. Adding it here just for completeness. Some readers may also find it instructive to look at the source code of this function.

declare @source varbinary(max);
set @source = 0x21232F297A57A5A743894A0E4A801FC3;
select varbin_source = @source
,string_result = master.dbo.fn_varbintohexstr (@source)
Overcash answered 19/12, 2013 at 9:21 Comment(0)
E
7

If you want to convert a single VARBINARY value into VARCHAR (STRING) you can do by declaring a variable like this:

DECLARE @var VARBINARY(MAX)
SET @var = 0x21232F297A57A5A743894A0E4A801FC3
SELECT CAST(@var AS VARCHAR(MAX))

If you are trying to select from table column then you can do like this:

SELECT CAST(myBinaryCol AS VARCHAR(MAX))
FROM myTable
Elurd answered 27/8, 2012 at 9:12 Comment(2)
For some reason on SQL Server 2012, Converting didnt work, but Cast did the trick??? Thanks!Fistic
Simple CAST may not work by default because the hex expression is converted to ASCII/UNICODE. Use CONVERT with a style flag (1 or 2).Kovar
S
3

This works in both SQL 2005 and 2008:

declare @source varbinary(max);
set @source = 0x21232F297A57A5A743894A0E4A801FC3;
select cast('' as xml).value('xs:hexBinary(sql:variable("@source"))', 'varchar(max)');
Sophiasophie answered 27/8, 2012 at 9:13 Comment(2)
How to do the same convert for field but not for variable?Satterfield
To do the same but for a column rather than a variable, you would use the sql:column function (technet.microsoft.com/en-us/library/ms191214.aspx). So it would be cast('' as xml).value('xs:hexBinary(sql:column("someColumnNameHere"))'Mistakable
P
2

I looked everywhere for an answer and finally this worked for me:

SELECT Lower(Substring(MASTER.dbo.Fn_varbintohexstr(0x21232F297A57A5A743894A0E4A801FC3), 3, 8000))

Outputs to (string):

21232f297a57a5a743894a0e4a801fc3

You can use it in your WHERE or JOIN conditions as well in case you want to compare/match varbinary records with strings

Pluckless answered 2/6, 2016 at 0:42 Comment(1)
Any idea on how to update that field back with the string re-converted back to varbinary?Popedom
F
0

Here is a simple example I wrote to convert and convert back using the 2 convert methods, I also checked it with a fixed string

declare @VB1 VARBINARY(500),@VB2 VARBINARY(500),@VB3 VARBINARY(500)

declare @S1 VARCHAR(500)

SET @VB1=HASHBYTES('SHA1','Test')

SET @S1=CONVERT(varchar(500),@VB1,2)

SET @VB2=CONVERT(varbinary(500),@S1,2)

SET @VB3=CONVERT(varbinary(500),'640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA',2)

SELECT @VB1,@S1,@VB2,@VB3

IF @VB1=@VB2 PRINT 'They Match(2)'

IF @VB1=@VB3 PRINT 'They Match(3)'

PRINT str(Len(@VB1))

PRINT str(Len(@S1))

PRINT str(Len(@VB2))

SET @VB1=HASHBYTES('SHA1','Test')

SET @S1=CONVERT(varchar(500),@VB1,1)

SET @VB2=CONVERT(varbinary(500),@S1,1)

SELECT @VB1,@S1,@VB2

IF @VB1=@VB2 PRINT 'They Match(1)'

PRINT str(Len(@VB1))

PRINT str(Len(@S1))

PRINT str(Len(@VB2))

and the output

||| 0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA

(1 row(s) affected)

They Match(2)

They Match(3)

    20
    40
    20

|| 0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA

(1 row(s) affected)

They Match(1)

    20
    42
    20
Fibrosis answered 27/2, 2017 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.