我正在尝试对SQL Server 2012中的变量进行编码/解码。我能够解码该变量,但是当我尝试使用解码后的值对其进行编码时,它没有显示相同的值。
DECLARE @DecodedVal VARCHAR(500)
DECLARE @EncodedVal VARCHAR(500)
DECLARE @NewDecodeVal VARCHAR(500)
--//decode
SET @DecodedVal= N'SW5mb3JtYXRpb25zIHN1ciBsZXMgY29uZsOpcmVuY2VzIHTDqWzDqXBob25pcXVlcw=='
SET @EncodedVal = (
SELECT CONVERT(VARCHAR(2000), CONVERT(XML, '<?xml version="1.0" encoding="UTF-8"?>' + CONVERT(VARCHAR(2000), CONVERT(XML, @DecodedVal).value('.', 'varbinary(max)'))))
)
SELECT @EncodedVal
--//encode
SET @NewDecodeVal = (SELECT CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:column("bin")))', 'NVARCHAR(MAX)') Base64Encoding
FROM ( SELECT CAST(@EncodedVal AS VARBINARY(MAX)) AS bin) AS bin_sql_server_temp)
SELECT @NewDecodeVal