2016-04-04 17 views
1

Ich möchte gesamte Spalte einer Tabelle in Base64-Zeichenfolge konvertieren.Wie konvertiert man die gesamte Spalte in Base64 String?

Zum Beispiel habe ich folgende Tabelle:

|Id |EmailID  | 
|123|[email protected]| 
|456|[email protected]| 

nun unter Ausgabe wie ich will:

|Id |EmailID  |Base64String     | 
|abc|[email protected]|Base64 string of (Id+EmailID)| 
|xyz|[email protected]|Base64 string of (Id+EmailID)| 

Jede Antwort sehr geschätzt wird.

Vielen Dank im Voraus.

+4

** [Base64-Codierung in SQL Server] (http://stackoverflow.com/questions/5082345/base64-encoding-in-sql-server-2005-t -sql) ** – lad2025

Antwort

0
;WITH cte AS (
SELECT * 
FROM (VALUES 
(123, '[email protected]'), 
(456, '[email protected]') 
) AS t(Id, EmailID) 
) 

SELECT Id, 
     EmailID, 
     CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:column("bin")))', 'NVARCHAR(MAX)') as Base64String 
FROM (
    SELECT Id, EmailID, CAST(CAST(Id as nvarchar(10)) + EmailID AS VARBINARY(MAX)) AS bin 
    FROM cte 
) as t 

Ausgang:

Id   EmailID  Base64String 
----------- -------------- ------------------------------------------------ 
123   [email protected] MQAyADMAdABlAHMAdAAxAEAAdABlAHMAdAAuAGMAbwBtAA== 
456   [email protected] NAA1ADYAdABlAHMAdAAyAEAAdABlAHMAdAAuAGMAbwBtAA== 

(2 row(s) affected) 
Verwandte Themen