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 native function exist? If not, what's the best way to do Base64 encoding in T-SQL?
相关问题
- Views base64 encoded blob in HTML with PHP
- Bulk update SQL Server C#
- POST Base64 encoded data in PHP
- SQL to Parse a Key-Value String
- ruby 1.9 wrong file encoding on windows
相关文章
- How to truncate seconds in TSQL?
- How to create base64Binary data?
- SQL identity (1,1) starting at 0
- Convert column to string in SQL Select
- SQL Group by Count of Counts
- How to generate sequential row number in tsql?
- Base64 Encoding: Illegal base64 character 3c
- Sql insert if row does not exist
Here's a modification to mercurial's answer that uses the subquery on the decode as well, allowing the use of variables in both instances.
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:
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!
No, there is no native function, this method has worked for me in the past: http://www.motobit.com/help/scptutl/sa306.htm
so has this method:
http://www.vbforums.com/showthread.php?t=554886
The simplest and shortest way I could find for SQL Server 2012 and above is
BINARY BASE64
:For Base64 to string
( or
nvarchar(max)
for Unicode strings )Here is the code for the functions that will do the work
I did a script to convert an existing hash encoded in base64 to decimal, it may be useful: