Wednesday, 22 May 2019

SQL Functions for Binary to Base64 and Base64 to Binary conversion

CREATE FUNCTION dbo.fn_BinaryToBase64(@binaryData VARBINARY(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN


    DECLARE @Base64 VARCHAR(MAX)


    SET @Base64 = CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:variable("@binaryData")))','VARCHAR(MAX)')

    RETURN @Base64


END
-----------------------------------------------------------------------------------


CREATE FUNCTION dbo.fn_Base64ToBinary(@Base64Data VARCHAR(MAX))
RETURNS VARBINARY(MAX)
AS

BEGIN

    DECLARE @Binary VARBINARY(MAX)

    SET @Binary = CAST(N'' AS XML).value('xs:base64Binary(sql:variable("@Base64Data"))', 'VARBINARY(MAX)')

    RETURN @Binary


END

No comments:

Post a Comment