I need a simple stored function to further encode the SQL Server database, but it returns null for every row I pass. I think sql: parameter does not work with input function parameter, but does not 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 piece of code works, but it declares local variables that cannot be executed in the 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
sql-server tsql encoding base64
Alex Zhukovskiy
source share