T-SQL - Convert binary data to/from Base64 (or other string representation)

16,058

Solution 1

Could not see this in the linked answers;

;with test(blob) as (select 0x776962626C65)

select blob from test
  for xml raw, binary base64

>>  <row blob="d2liYmxl"/>

Solution 2

I had an old bookmark to this site that converts it using an xml conversion not really straightforward but it works.: http://blogs.msdn.com/b/sqltips/archive/2008/06/30/converting-from-base64-to-varbinary-and-vice-versa.aspx

Share:
16,058
Ryan
Author by

Ryan

Developer working primarily with SharePoint

Updated on July 18, 2022

Comments

  • Ryan
    Ryan almost 2 years

    Is there a Microsoft T-SQL statement that will allow you to convert a binary data type (like image) to/from a string representation like Base64.

    its easy enough in .net (Convert.ToBase64 & Convert.FromBase64) but is this possible using SQL alone, something like (obviously pseudocode)

    SELECT CastAsBase64(binary_field) As Base64BinaryField
    FROM   SomeTable
    WHERE  ID = @ID
    
  • Shujaath Khan
    Shujaath Khan over 8 years
    perfect solution to convert binary img data to base64 string