DEV Community

loading...

Base64 Conversions in SQL Server

Joe Enos
Software developer/architect, mostly working with .NET and SQL Server.
・1 min read

SQL Server has the ability to base-64 encode or decode, but the way it does it is really kind of funky.

.NET has nice simple Convert.ToBase64String and Convert.FromBase64String methods, but it's a little more work in SQL Server.

Converting from base 64 to bytes:

CAST(@Input AS XML).value('.', 'VARBINARY(MAX)');
Enter fullscreen mode Exit fullscreen mode

Convert from bytes to base 64:

SELECT @Input FOR XML PATH(''), BINARY BASE64
Enter fullscreen mode Exit fullscreen mode

Discussion (0)