[SQL Server] Convertir une chaîne en BASE64

 

/*** Exemple N° 1 ***/

DECLARE @chaine_source_1          VARBINARY(MAX), 

             @code_base64_1      VARCHAR(MAX), 

             @decode_base64_1    VARBINARY(MAX)

 

SET @chaine_source_1 = CONVERT(VARBINARY (MAX), 'mektaba.info')

-- Convertir VARBINARY en chaine BASE64

SET @code_base64_1 = CAST(N'' AS xml).value('xs:base64Binary(sql:variable("@chaine_source_1"))', 'varchar(MAX)')

-- Convertir chaine BASE64 en VARBINARY

SET @decode_base64_1 = CAST(N'' AS xml).value('xs:base64Binary(sql:variable ("@code_base64_1"))', 'varbinary(MAX)')

 

 SELECT

CONVERT(varchar(MAX), @chaine_source_1) AS Chaine_Source,

@chaine_source_1 AS varbinary_source,

@code_base64_1 AS code_base64,

@decode_base64_1 AS decode_varbinary,

CONVERT(varchar(MAX), @decode_base64_1) AS decode_varchar

 

/*** Exemple N° 2 ***/

DECLARE @Chaine_Source            VARCHAR(100) = 'mektaba.info',

             @codee_base64        VARCHAR(500),

             @decode_base64      VARCHAR(200)   

 

SELECT @codee_base64 =

    CAST(N'' AS XML).value(

          'xs:base64Binary(xs:hexBinary(sql:column("bin")))'

        , 'VARCHAR(MAX)'

    )

FROM (

    SELECT CAST(@Chaine_Source AS VARBINARY(MAX)) AS bin

) AS bin_sql_server_temp;

 

 

SELECT @decode_base64 =

CAST(

    CAST(N'' AS XML).value(

        'xs:base64Binary(sql:column("bin"))'

      , 'VARBINARY(MAX)'

    )

    AS VARCHAR(MAX)

)

FROM (

    SELECT CAST(@codee_base64 AS VARCHAR(MAX)) AS bin

) AS bin_sql_server_temp;

 

 

SELECT @Chaine_Source,@codee_base64 AS codee_base64,@decode_base64 AS decode_base64