title | description | author | ms.author | ms.reviewer | ms.date | ms.service | ms.subservice | ms.topic | f1_keywords | helpviewer_keywords | dev_langs | monikerRange | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
BASE64_DECODE (Transact-SQL) |
BASE64_DECODE converts a base64 encoded varchar into the corresponding varbinary. |
abledenthusiast |
aaronpitman |
wiassaf, randolphwest |
02/28/2025 |
sql |
t-sql |
reference |
|
|
|
=azuresqldb-current||=fabric |
[!INCLUDE asdb-fabric-se-and-dw]
BASE64_DECODE
converts a base64-encoded varchar expression into the corresponding varbinary expression.
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions
BASE64_DECODE ( expression )
An expression of type varchar(n) or varchar(max).
- varbinary(8000) if the input is varchar(n).
- varbinary(max) if the input is varchar(max).
- If the input expression is
NULL
, the output isNULL
.
The encoded string's alphabet must be that of RFC 4648 Table 1 and might include padding, though padding isn't required. The URL-safe alphabet specified within RFC 4648 Table 2 is also accepted. This function ignores whitespace characters: \n
, \r
, \t
, and
.
-
When the input contains characters not contained within the standard or URL-safe alphabets specified by RFC 4648, the function returns the following error:
Msg 9803, Level 16, State 20, Line 15, Invalid data for type "Base64Decode"
-
If the data has valid characters, but incorrectly formatted, the function returns error
Msg 9803, State 21
. -
If the input contains more than two padding characters or padding characters followed by extra valid input the function returns error
Msg 9803, State 23
.
In the following example, the base64 encoded string is decoded back into varbinary.
SELECT BASE64_DECODE('qQ==');
[!INCLUDE ssResult_md]
0xA9
In the following example, the string is base64 decoded. Note the string contains URL-unsafe characters =
and /
.
SELECT BASE64_DECODE('yv7K/g==');
[!INCLUDE ssResult_md]
0xCAFECAFE
In contrast to example B, this example base64 string was encoded using RFC 4648 Table 2 (url_safe
), but can be decoded the same way as example B.
SELECT BASE64_DECODE('yv7K_g');
[!INCLUDE ssResult_md]
0xCAFECAFE
This example contains characters that aren't valid base64 characters.
SELECT BASE64_DECODE('qQ!!');
[!INCLUDE ssResult_md]
Msg 9803, Level 16, State 20, Line 223
Invalid data for type "Base64Decode".