Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server converting varbinary to string

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?

like image 779
strike_noir Avatar asked Aug 27 '12 09:08

strike_noir


People also ask

Is VARBINARY a string?

A binary string is a sequence of octets or bytes. BYTEA and RAW are synonyms for VARBINARY.

What is VARBINARY in SQL Server?

varbinary [ ( n | max) ] Variable-length binary data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes. The data that is entered can be 0 bytes in length.

Can we convert varchar to VARBINARY in SQL Server?

SqlException: 'Implicit conversion from data type varchar to varbinary(max) is not allowed.


1 Answers

Try:

DECLARE @varbinaryField varbinary(max); SET @varbinaryField = 0x21232F297A57A5A743894A0E4A801FC3;  SELECT CONVERT(varchar(max),@varbinaryField,2),  @varbinaryField 

UPDATED: For SQL Server 2008

like image 142
András Ottó Avatar answered Sep 16 '22 17:09

András Ottó