Refactoring and static code analysis tool for SQL Server
Got a SQL Server or .NET question? Discuss it in the forums. (SQL Server Forums | Dot NET Forums)
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

Converting VARBINARY to VARCHAR using FOR XML

We have seen a number of string manipulation examples using XML. Here is yet another example that uses FOR XML to convert a VARBINARY value to VARCHAR.

Some one asked me this question in one of the forums. The story is like this. The OP did a data migration project and the original data from the ORACLE database is imported to a SQL Server database. After the migration, he noticed that one of the VARCHAR columns in the source database is imported as VARBINARY column in SQL Server. Now he wanted to convert the VARBINARY value back to VARCHAR.

Here is a simple example that demonstrates the case.

DECLARE @x VARBINARY(10)
SELECT @x = CAST('10' as VARBINARY(10))
SELECT @x AS VarBinaryValue

/*
VarBinaryValue
----------------------
0x3130
*/

The original value was ‘10’ which was converted to VARBINARY and the result is ‘0x3130’. We need to write a query to convert ‘0x3130’ back to ‘10’.

I could not find an easy method to achieve this. I wrote a quick and dirty piece of code using FOR XML to achieve this.

DECLARE @x VARBINARY(10)
SELECT @x = CAST('10' as VARBINARY(10))
SELECT @x AS VarBinaryValue

/*
VarBinaryValue
----------------------
0x3130
*/

SELECT (
SELECT
CHAR(SUBSTRING(@x,number,1)) AS 'text()'
FROM master..spt_values
WHERE type = 'P'
AND Number BETWEEN 1 AND LEN(@x)
FOR XML PATH('')
) AS TextValue

/*
TextValue
-------------------------------------------
10
*/

Update on 14 June 2009

RBarry commented that this can be achieved by a simple CAST back to VARCHAR. Yes, it works! I wonder what went wrong when I tested it initially and could not get back the original value. Anyway, the XML approach is not needed any more. A simple cast will do the trick as shown in the below example.

DECLARE @x VARBINARY(10)
SELECT @x = CAST('10' as VARBINARY(10))

SELECT
@x AS VarBinaryValue,
CAST(@x AS VARCHAR(10)) AS VarcharValue

/*
VarBinaryValue VarcharValue
---------------------- ------------------------------
0x3130 10
*/

FOR XML Tutorials

If you like this article,  Subscribe in a reader or Subscribe by Email. Show your support by sharing this article with your friends through the services given below.

Share

Comments

# re: Converting VARBINARY to VARCHAR using FOR XML

Saturday, June 13, 2009 11:11 PM by R Barry Young

All you have to do is cast the Varbinary() back to Varchar().  I do it all the time for ServiceBroker:

   SELECT CAST(@x as Varchar(MAX))

# re: Converting VARBINARY to VARCHAR using FOR XML

Sunday, June 14, 2009 4:44 AM by Jacob Sebastian

Thank you RBarry,

I see that casting back to VARCHAR works!. (Not sure what went wrong when I tested it earlier).

Thank you for pointing it out.

DECLARE @x VARBINARY(10)

SELECT  @x = CAST('10' as VARBINARY(10))

SELECT

@x AS VarBinaryValue,

CAST(@x AS VARCHAR(10)) AS VarcharValue

/*

VarBinaryValue         VarcharValue

---------------------- ------------------------------

0x3130                 10

*/

Copyright © Beyondrelational.com