Encode Function
Decode Function
/* Base64 Encode */
CREATE FUNCTION [dbo].[f_Base64_Encode]
(
@var VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Base64 VARCHAR(MAX)
DECLARE @bin VARBINARY(MAX)
SET @bin=CONVERT(VARBINARY(MAX),@var)
SET @Base64 = CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:variable("@bin")))', 'VARCHAR(MAX)')
RETURN @Base64
END
Decode Function
/* Base64 Decode */
CREATE FUNCTION [dbo].[f_Base64_Decode]
(
@Base64 VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Bin VARBINARY(MAX)
DECLare @Var VARCHAR(MAX)
SET @Base64=
CASE
WHEN len(@Base64)%3=2 THEN @Base64+'=='
WHEN len(@Base64)%3=1 THEN @Base64+'='
END
SET @Bin = CAST(N'' AS XML).value('xs:base64Binary(sql:variable("@Base64"))', 'VARBINARY(MAX)')
SET @Var=CONVERT(VARCHAR(MAX),@Bin)
RETURN @Var
END
GO
How to Use
'----------------------'
SELECT dbo.f_Base64_Encode('0926518918') AS Encode
SELECT dbo.f_Base64_Decode('MDkyNjUxODkxOA') AS Decode
留言