February 13, 2010

Convert Character Data to Binary and Vice-Versa in SQL Server 2008

I was recently working on a query where I had to convert a Character data to Binary and then later convert the Binary to Character data back again. Here’s how to do it.

We will assume that the character data with us is ‘SQLServer’

-- Convert Character to Binary
SELECT CONVERT(binary(9), 'SQLServer', 0) AS 'Char to Binary'

-- Convert Binary to Character
SELECT CONVERT(char(9), 0x53514C536572766572, 0) AS 'Binary to Char'



About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

No comments: