﻿  IF OBJECT_ID('dbo.fnConvertBase64') IS NOT NULL BEGIN
    DROP FUNCTION dbo.fnConvertBase64
  END
  GO

  CREATE FUNCTION dbo.fnConvertBase64 (@source VARBINARY(MAX))
    RETURNS NVARCHAR(64)
  BEGIN    
    RETURN CAST('' AS XML).value('xs:base64Binary(sql:variable(''@source''))', 'varchar(max)')
  END;
  GO