0

Is there any simple function for get this output.

SELECT Title FROM Table WHERE Id = 1 
---- My title

Desire function or etc ...

SELECT MYBASE64ENCODE(Title) FROM Table WHERE Id = 1 
---- TXkgdGl0bGU=

I need the function for ecnode string to base64.

If answer is this so how can put my column as input of my query.

Community
  • 1
  • 1

1 Answers1

3

Maybe create a User-defined function something like this.....

CREATE FUNCTION dbo.get_base64Coded (@Input nvarchar(4000))
RETURNS  nvarchar(4000)
AS
BEGIN
  Declare @Output nvarchar(4000);

SELECT
   @Output = CAST(N'' AS XML).value(
          'xs:base64Binary(xs:hexBinary(sql:column("value")))'
        , 'VARCHAR(MAX)')   
FROM (
       SELECT CAST(@Input AS VARBINARY(MAX)) AS value
     ) AS A;
  RETURN @Output      
END

Your query would look something like...

SELECT dbo.get_base64Coded(Title) FROM Table WHERE Id = 1 
M.Ali
  • 67,945
  • 13
  • 101
  • 127
  • Alternative function body, `DECLARE @InputBinary varbinary(MAX) = CAST(@Input AS varbinary(MAX)); RETURN CAST(N'' AS XML).value( 'xs:base64Binary(xs:hexBinary(sql:variable("@InputBinary")))' , 'VARCHAR(4000)');` – Dan Guzman May 21 '16 at 14:40