עריכה

שתף דרך


BASE64_DECODE (Transact-SQL)

Applies to: SQL Server 2025 (17.x) Azure SQL Database Azure SQL Managed Instance SQL analytics endpoint in Microsoft Fabric and Warehouse in Microsoft Fabric SQL database in Microsoft Fabric

BASE64_DECODE converts a Base64-encoded varchar expression into the corresponding varbinary expression.

Transact-SQL syntax conventions

Syntax

BASE64_DECODE ( expression )

Arguments

expression

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

Return types

  • varbinary(8000) if the input is varchar(n).
  • varbinary(max) if the input is varchar(max).
  • If the input expression is NULL, the output is NULL.

Remarks

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 is 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.

Examples

A. Standard BASE64_DECODE

In the following example, the Base64 encoded string is decoded back into varbinary.

SELECT BASE64_DECODE('qQ==');

Here's the result set.

0xA9

B. BASE64_DECODE a standard Base64 string

In the following example, the string is Base64 decoded. Note the string contains URL-unsafe characters = and /.

SELECT BASE64_DECODE('yv7K/g==');

Here's the result set.

0xCAFECAFE

C. BASE64_DECODE varchar url_safe Base64 string

Unlike example B, this example uses RFC 4648 Table 2 (url_safe) to encode the Base64 string. However, you can decode it the same way as example B.

SELECT BASE64_DECODE('yv7K_g');

Here's the result set.

0xCAFECAFE

D. BASE64_DECODE varchar contains characters not in the Base64 alphabet

This example contains characters that aren't valid Base64 characters.

SELECT BASE64_DECODE('qQ!!');

Here's the result set.

Msg 9803, Level 16, State 20, Line 223
Invalid data for type "Base64Decode".