Getting Started with ASP.NET MVC - Part 6: ASP.NET MVC and Entity Framework
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.


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
XQuery 69
TSQL 67
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
August 2009 19
June 2009 19
May 2010 18
January 2009 15
July 2008 15
January 2010 14
October 2008 14
February 2010 12

Converting VARBINARY to VARCHAR using FOR XML

Jun 13 2009 9:09AM by Jacob Sebastian   

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

Tags: XML, String-manipulation-using-xml, FOR XML PATH, FOR XML,


Jacob Sebastian
1 · 100% · 22504
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • 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))
    
    commented on Jun 13 2009 11:11PM  .  Report Abuse This post is not formatted correctly
    R Barry Young
    1542 · 0% · 6
  • 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 */

    commented on Jun 14 2009 4:44AM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22504

Your Comment


Sign Up or Login to post a comment.

    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising