Base64 encoding in SQL Server 2005 T-SQL

后端 未结 10 801
傲寒
傲寒 2020-11-22 10:06

I\'d like to write a T-SQL query where I encode a string as a Base64 string. Surprisingly, I can\'t find any native T-SQL functions for doing Base64 encoding. Does a nativ

相关标签:
10条回答
  • 2020-11-22 10:30

    The simplest and shortest way I could find for SQL Server 2012 and above is BINARY BASE64 :

    SELECT CAST('string' as varbinary(max)) FOR XML PATH(''), BINARY BASE64
    

    For Base64 to string

    SELECT CAST( CAST( 'c3RyaW5n' as XML ).value('.','varbinary(max)') AS varchar(max) )
    

    ( or nvarchar(max) for Unicode strings )

    0 讨论(0)
  • 2020-11-22 10:32

    I know this has already been answered, but I just spent more time than I care to admit coming up with single-line SQL statements to accomplish this, so I'll share them here in case anyone else needs to do the same:

    -- Encode the string "TestData" in Base64 to get "VGVzdERhdGE="
    SELECT
        CAST(N'' AS XML).value(
              'xs:base64Binary(xs:hexBinary(sql:column("bin")))'
            , 'VARCHAR(MAX)'
        )   Base64Encoding
    FROM (
        SELECT CAST('TestData' AS VARBINARY(MAX)) AS bin
    ) AS bin_sql_server_temp;
    
    -- Decode the Base64-encoded string "VGVzdERhdGE=" to get back "TestData"
    SELECT 
        CAST(
            CAST(N'' AS XML).value(
                'xs:base64Binary("VGVzdERhdGE=")'
              , 'VARBINARY(MAX)'
            ) 
            AS VARCHAR(MAX)
        )   ASCIIEncoding
    ;
    

    I had to use a subquery-generated table in the first (encoding) query because I couldn't find any way to convert the original value ("TestData") to its hex string representation ("5465737444617461") to include as the argument to xs:hexBinary() in the XQuery statement.

    I hope this helps someone!

    0 讨论(0)
  • 2020-11-22 10:33

    You can use just:

    Declare @pass2 binary(32)
    Set @pass2 =0x4D006A00450034004E0071006B00350000000000000000000000000000000000
    SELECT CONVERT(NVARCHAR(16), @pass2)
    

    then after encoding you'll receive text 'MjE4Nqk5'

    0 讨论(0)
  • 2020-11-22 10:39
    DECLARE @source varbinary(max),  
    @encoded_base64 varchar(max),  
    @decoded varbinary(max) 
    SET @source = CONVERT(varbinary(max), 'welcome') 
    -- Convert from varbinary to base64 string 
    SET @encoded_base64 = CAST(N'' AS xml).value('xs:base64Binary(sql:variable       
    ("@source"))', 'varchar(max)') 
      -- Convert back from base64 to varbinary 
       SET @decoded = CAST(N'' AS xml).value('xs:base64Binary(sql:variable             
      ("@encoded_base64"))', 'varbinary(max)') 
    
     SELECT
      CONVERT(varchar(max), @source) AS [Source varchar], 
       @source AS [Source varbinary], 
         @encoded_base64 AS [Encoded base64], 
         @decoded AS [Decoded varbinary], 
         CONVERT(varchar(max), @decoded) AS [Decoded varchar]
    

    This is usefull for encode and decode.

    By Bharat J

    0 讨论(0)
提交回复
热议问题