Base64 encoding in SQL Server 2005 T-SQL

后端 未结 10 799
傲寒
傲寒 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 )

提交回复
热议问题