-- dbo.ef_EncodeBase64
CREATE FUNCTION dbo.ef_EncodeBase64 (@source AS varchar(max))
 RETURNS varchar(max)
AS
 BEGIN
  DECLARE @bin varbinary(max), @result varchar(max)
  SET @bin = CONVERT(varbinary(max), @source)
  RETURN CAST(N'' as xml).value('xs:base64Binary(xs:hexBinary(sql:variable("@bin")))', 'varchar(max)')
 END
