-- ============================================= -- Author:-- Create date: <20090123> -- Description: -- ============================================= CREATE FUNCTION dbo.f_BinaryToBase64 ( @bin VARBINARY(MAX) ) RETURNS VARCHAR(MAX) AS BEGIN DECLARE @Base64 VARCHAR(MAX) /* SELECT dbo.f_BinaryToBase64(CONVERT(VARBINARY(MAX), 'Converting this text to Base64...')) */ SET @Base64 = CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:variable("@bin")))', 'VARCHAR(MAX)') RETURN @Base64 END -- ============================================= -- Author: 20090123>-- Create date: <20090123> -- Description: -- ============================================= CREATE FUNCTION dbo.f_Base64ToBinary ( @Base64 VARCHAR(MAX) ) RETURNS VARBINARY(MAX) AS BEGIN DECLARE @Bin VARBINARY(MAX) /* SELECT CONVERT(VARCHAR(MAX), dbo.f_Base64ToBinary('Q29udmVydGluZyB0aGlzIHRleHQgdG8gQmFzZTY0Li4u')) */ SET @Bin = CAST(N'' AS XML).value('xs:base64Binary(sql:variable("@Base64"))', 'VARBINARY(MAX)') RETURN @Bin END 20090123>
-- ============================================= -- VARCHAR -- ============================================= DECLARE @Base64Data VARCHAR(MAX) SET @Base64Data = dbo.f_BinaryToBase64(CONVERT(VARBINARY(MAX),'testing...')) SELECT @Base64Data AS Base64, CONVERT(VARCHAR(MAX), dbo.f_Base64ToBinary(@Base64Data)) AS My_Data
-- ============================================= -- BIGINT -- ============================================= DECLARE @BigIntData BIGINT DECLARE @Base64Data VARCHAR(MAX) SET @BigIntData = 1242353245346345643 SET @Base64Data = dbo.f_BinaryToBase64(CONVERT(VARBINARY(MAX), @BigIntData)) SELECT @Base64Data AS Base64, CONVERT(BIGINT, dbo.f_Base64ToBinary(@Base64Data)) AS [BigInt]
-- ============================================= -- DATETIME -- ============================================= DECLARE @myDate DATETIME DECLARE @Base64Data VARCHAR(MAX) SET @myDate = GETDATE() SET @Base64Data = dbo.f_BinaryToBase64(CONVERT(VARBINARY(MAX), @myDate)) SELECT @Base64Data AS Base64, CONVERT(DATETIME, dbo.f_Base64ToBinary(@Base64Data)) AS myDate
-- ============================================= -- XML -- ============================================= DECLARE @XML XML DECLARE @Base64Data VARCHAR(MAX) SET @XML = ( SELECT TABLE_NAME, ORDINAL_POSITION, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS AS row WHERE TABLE_NAME = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME NOT LIKE 'sys%') FOR XML AUTO, ELEMENTS, ROOT('root') ) SET @Base64Data = dbo.f_BinaryToBase64(CONVERT(VARBINARY(MAX), @XML)) SELECT @Base64Data AS [Base64] DECLARE @New_XML XML SET @New_XML = CONVERT(XML, dbo.f_Base64ToBinary(@Base64Data)) SELECT row.col.value('./TABLE_NAME[1]','NVARCHAR(128)') AS TABLE_NAME , row.col.value('./ORDINAL_POSITION[1]','INT') AS ORDINAL_POSITION , row.col.value('./COLUMN_NAME[1]','NVARCHAR(128)') AS COLUMN_NAME FROM @New_XML.nodes('//row') AS row(col)
(http://www.vbforums.com/showthread.php?554886-SQL-Server-2005-Convert-any-data-to-Base64-and-back)
Không có nhận xét nào:
Đăng nhận xét