SQL Server converting varbinary to string

strike_noir picture strike_noir · Aug 27, 2012 · Viewed 218.5k times · Source

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?

Answer

András Ottó picture András Ottó · Aug 27, 2012

Try:

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

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

UPDATED: For SQL Server 2008