Retrieve varbinary value as BASE64 in MSSQL

convert varbinary to base64 online
sql server varbinary to string
varbinary to base64 online converter
convert varbinary to image sql server
base64 to varbinary online
sql server insert base64 into varbinary
sql server convert text to varbinary
sql server convert hex to base64

I'm looking for a way for retrieving Entity Data Model (EDM) from __MigrationHistory table using only T-SQL (so anyone, using Microsoft SQL Server Management Studio only, could do the same).

I want to have a valid BASE64 string value.

I don't want to fully decompress it to EDMX.

I don't want to get it from *.resx migration file.

When I select whole __MigrationHistory in SSMS, Model column value is like following


This is my sample database, so I don't mind sharing it.

The column is of type varbinary(max), I googled how can convert it to UTF8*-ish* text.

varbinary to string on SQL Server

SQL Server: Convert a string into a hex string and back #sql #sqlserver


SELECT CONVERT(VARCHAR(max), 0x1F8B0800000000000400CD57DB6EDB3810..... , 0)

or even more straightforward

SELECT CONVERT(VARCHAR(max), (SELECT TOP (1) [t0].[Model]FROM [__MigrationHistory] AS [t0]), 0);

I get the following result

literally this:

I checked my Model if it's not broken or incomplete in both LinqPad5 and using this tool found here, but it looks ok.

Finally found this article:

So, running the query gets what I wanted, valid Base64.

Using XML and the hint "for xml path"

select Model, baze64
from __MigrationHistory
cross apply (select Model as '*' for xml path('')) T (baze64)

Other presented queries in article will also work

Using XML XQuery

Using JSON

Converting Column values from VARBINARY to Base64 You can get the binary value of your image using simple select from our table (we  APPLIES TO: SQL Server Azure SQL Database Azure Synapse Analytics (SQL DW) Parallel Data Warehouse . If the BINARY BASE64 option is specified in the query, the binary data is returned in base64 encoding format. If the BINARY BASE64 option is not specified in the query, then by default, AUTO mode supports URL encoding of binary data.

It's apparently gzip'd xml. eg

SELECT MigrationId
      ,cast(decompress(model) as xml) model
  FROM __MigrationHistory

Convert VARBINARY (BLOB) to Base64 String using T-SQL. Question asked by TLongSUGF on Jul 12, 2017. Latest reply on Jan 5, 2018 by TLongSUGF. In this question I asked how can I convert varbinary to valid BASE64 string value in MSSQL. Now I'd like to know how can it be done without MSSQL (in .NET f.e.). Let's think of a method which accepts the following string (or byte array) and returns valid BASE64

Necromancing. You can also use for JSON PATH similar to how for XML PATH is used:

    ,NULLIF(T.XML_Thumbnail, '') AS base64  

    ,COMPRESS(T_AP_Dokumente.DK_Thumbnail) AS gzipped 
    ,DATALENGTH(COMPRESS(T_AP_Dokumente.DK_Thumbnail)) AS dlt_datalength_gzipped 
    ,DATALENGTH(T_AP_Dokumente.DK_Thumbnail) AS dlt_datalength 
    ,DATALENGTH(DECOMPRESS(COMPRESS(T_AP_Dokumente.DK_Thumbnail))) AS should_equal_dlt_datalength 
FROM T_AP_Dokumente 

    SELECT T_AP_Dokumente.DK_Thumbnail AS '*' FOR XML PATH('')
) AS T(XML_Thumbnail)

            SELECT T_AP_Dokumente.DK_Thumbnail AS JSON_Thumbnail 
            FOR JSON PATH 
    ) WITH(JSON_Thumbnail varchar(MAX)) AS t 
) AS tBase64 

set @source = convert(varbinary(max), 'Hello Base64') set @encoded = cast('' as xml).value('xs:base64Binary(sql:variable(“@source”))',  The following query returns the product photo stored in a varbinary(max) type column. The BINARY BASE64 option is specified in the query to return the binary data in base64-encoded format. Example USE AdventureWorks2012; GO SELECT ProductPhotoID, ThumbNailPhoto FROM Production.ProductPhoto WHERE ProductPhotoID=1 FOR XML RAW, BINARY BASE64 ; GO

In this tip we look at how to decode Base64 encoded text stored in a SQL Server table and SQL Server CONVERT() and CAST() function do not recognize Base64 textual 'VARBINARY(MAX)') AS img FROM ConvertBase64 Specify column [img] in the Extract Column since this contains the binary  Function for encode string to Base64 in sql server/ Function for edecode Base64string to String in sql server. Previous Updates In previous articles we have learnt Best basic C# interview questions. Transform multiple rows into one comma separated string column. .AngularJS basics, What is Cursor in sql and use of cursor.

SQL Server uses UTF-16 Little Endian only for NVARCHAR data, and even for XML. The trick is to convert the Base64 decoded bytes, in their text representation (even VARBINARY(500) = CONVERT(XML, @Base64Value).​value('.','varbinary(max)'); DECLARE @IntermediaryValue VARCHAR(500)  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to Store and Retrieve a varbinary(max) Column in SQL Server

Figured it out: SELECT CONVERT ( VARCHAR(MAX), CAST('' AS XML).value('xs​:base64Binary(sql:column("BASE64_COLUMN"))',  Did you know that SQL Server has built-in support for base64 encoding and decoding and has since 2005? I sure didn’t, because it’s hidden in the depths of XQuery. Here is a complete sample

  • This is ever simpler solution. I wonder though why I get different results in SMSS and LinqPad5. Any ideas?