Michael Rys

Musings on XML, XQuery and more...

<August 2008>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456


Navigation

Papers

SQL Server XML Whitepapers

Weblogging Links

MS Bloggers

Recommended Books

Other Blogs

Recommended Links

Presentations (Upcoming)

Presentations (Recent)

Subscriptions

News


Upcoming Presentations


TechEd 2007, Orlando, June 4 to June 8, 2007


Books I co-authored



www.flickr.com
This is a Flickr badge showing public photos from Michael Rys. Make your own badge here.
eXTReMe Tracker

Post Categories

Article Categories



How to use base64 encoding in SQL Server 2005

Kirk Allen Evans gives an example how to generate a base64 encoded WordML binData element. Just for kicks, here is how the same code would look like in SQL Server 2005 using FOR XML (as in his case, it is not the complete Word document):

WITH XMLNAMESPACES ('http://schemas.microsoft.com/office/word/2003/wordml' as w)
SELECT img as "w:binData"
FROM
OpenRowset(BULK 'c:\temp\test.gif', SINGLE_BLOB) T(img)
FOR XML PATH('w:wordDocument'), TYPE

Oh the joy of declarative programming :-). Here is how you can extract the value (assuming the XML with the base64 encoded value was in a T-SQL variable @x):

SELECT @x.value('declare namespace w="http://schemas.microsoft.com/office/word/2003/wordml";
(/w:wordDocument/w:binData)[1]', 'varbinary(max)')

Note that the value() method will automatically base64 decode (or also hex decode, if the binary value was of type xs:hexBinary) if the target SQL type is a binary type.

posted on Monday, April 18, 2005 8:34 PM by mrys


# re: How to use base64 encoding in SQL Server 2005 @ Wednesday, November 29, 2006 2:17 PM

This works fine for Encoding base64Binary xml, but value() does NOT automatically decode base64Binary. for example, I am creating XML in a storedproc like this:

DECLARE @BinDecoded varbinary(max)
set @x = Cast('<row q="' + @qsEncoded + '" />' as xml)
SET @BinDecoded = (SELECT @x.value('(/row/@q)[1]','varbinary(max)'))
SET @qsDecoded = Convert(varchar(max), @BinDecoded)

Where @qsEncoded is base64 string. @qsDecoded is not decoded because @BinDecoded is not automatically stored in the varbinary correctly. The base64 is just converted straight to binary and stored as-is.

Steele Price

# re: How to use base64 encoding in SQL Server 2005 @ Tuesday, January 23, 2007 10:38 PM

I am not sure I understand your point.

The following is a complete script:

DECLARE @qsEncoded varchar(max), @qsDecoded varchar(max);
DECLARE @BinDecoded varbinary(max);
DECLARE @x xml;
set @qsEncoded = (SELECT img as "text()"
FROM OpenRowset(BULK 'c:\temp\test.gif', SINGLE_BLOB) T(img)
FOR XML PATH(''));
select @qsEncoded;
set @x = Cast('<row q="' + @qsEncoded + '" />' as xml)
select @x;
SET @BinDecoded = (SELECT @x.value('(/row/@q)[1]','varbinary(max)'))
select @BinDecoded;
SET @qsDecoded = Convert(varchar(max), @BinDecoded)
select @qsDecoded;

It clearly shows that the @BinDecoded indeed contains the binary representation of the value after being base64 decoded. And @qsDecoded contains the decoded string version and not the base64 encoded string.

Best regards
Michael

mrys




Powered by Dot Net Junkies, by Telligent Systems