Skip to content

Latest commit

 

History

History
120 lines (81 loc) · 3.39 KB

base64-encode-transact-sql.md

File metadata and controls

120 lines (81 loc) · 3.39 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs monikerRange
BASE64_ENCODE (Transact-SQL)
BASE64_ENCODE converts the value of a varbinary into a base64 encoded varchar.
abledenthusiast
aaronpitman
wiassaf, randolphwest
02/28/2025
sql
t-sql
reference
BASE64_ENCODE
BASE64_ENCODE_TSQL
base64 encode [SQL Server], base64 encode
BASE64_ENCODE function
base64 encoding [SQL Server]
TSQL
=azuresqldb-current||=fabric

BASE64_ENCODE (Transact-SQL)

[!INCLUDE asdb-fabric-se-and-dw]

BASE64_ENCODE converts the value of a varbinary expression into a base64-encoded varchar expression.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

BASE64_ENCODE (expression [ , url_safe ] )

Arguments

expression

An expression of type varbinary(n) or varbinary(max).

url_safe

Optional integer literal or expression, which specifies whether the output of the encode operation should be URL-safe. Any number other than 0 evaluates to true. The default value is 0.

Return types

  • varchar(8000) if the input is varbinary(n) where n <= 6000.
  • varchar(max) if the input is varbinary(n) where n > 6000.
  • varchar(max) if the input is varbinary(max).
  • If the input expression is NULL, the output is NULL.

Remarks

The encoded string alphabet is that of RFC 4648 Table 1 and might add padding. The URL-safe output uses the base64url alphabet of RFC 4648 Table 2 and doesn't add padding. This function doesn't add any new line characters.

In each case, the database default collation is used. For more information on the supported collations in [!INCLUDE fabric], see Tables.

If url_safe is true, the base64url string that is generated is incompatible with SQL Server's XML and JSON base64 decoders.

Examples

A. Standard BASE64_ENCODE

The following example returns the base64 encoded value for the © symbol.

SELECT Base64_Encode(0xA9) AS "Encoded © symbol";

[!INCLUDE ssResult_md]

qQ==

B. BASE64_ENCODE a string

In the following example, a string is base64 encoded. The string must first be casted to a varbinary.

SELECT BASE64_ENCODE(CAST ('hello world' AS VARBINARY));

[!INCLUDE ssResult_md]

aGVsbG8gd29ybGQ=

C. BASE64_ENCODE default vs url_safe

In the following example, the first select doesn't specify url_safe; however, the second select does specify url_safe.

SELECT BASE64_ENCODE(0xCAFECAFE);

[!INCLUDE ssResult_md]

yv7K/g==

The following example specifies that the output is URL-safe.

SELECT BASE64_ENCODE(0xCAFECAFE, 1);

[!INCLUDE ssResult_md]

yv7K_g

Related content