I need a simple stored function for further SQL Server base64 encoding, but it returns null for every string I pass. I think that sql:parameter is not working with input function parameter, but don't know how to avoid it.
This is my code:
ALTER FUNCTION [dbo].[usf_base64_encode]
(@value nvarchar(max))
RETURNS nvarchar(max)
AS
BEGIN
RETURN cast('' as xml).value('xs:base64Binary(sql:variable("@value"))', 'varchar(max)')
END
This code snippet works, but it declares local variables that's impossible to do in function.
declare @source varbinary(max), @encoded varchar(max), @decoded varbinary(max)
set @source = convert(varbinary(max), 'Hello Base64')
set @encoded = cast('' as xml).value('xs:base64Binary(sql:variable("@source"))', 'varchar(max)')
set @decoded = cast('' as xml).value('xs:base64Binary(sql:variable("@encoded"))', 'varbinary(max)')
select
convert(varchar(max), @source) as source_varchar,
@source as source_binary,
@encoded as encoded,
@decoded as decoded_binary,
convert(varchar(max), @decoded) as decoded_varchar