// strings reference

SQL base64 encoding

Encodes a string or binary value as Base64 text for transport through text-only channels.

Categoría
Strings
Returns
string
In this category
131 of 172

Sintaxis

TO_BASE64(string)
PostgreSQL: encode(bytes, 'base64')

Each block is a separate form. The support table and the examples show which engine takes which.

Soporte del motor

MotorSupportNotas
SQL Server Different name or syntax BASE64_ENCODE(varbinary) in SQL Server 2025 and Azure SQL; older versions use FOR XML tricks.
PostgreSQL Different name or syntax encode(convert_to(str, 'UTF8'), 'base64').
MySQL Sí TO_BASE64 is built in.
SQLite Not built in No Base64 built in.
Oracle Different name or syntax UTL_ENCODE.BASE64_ENCODE on RAW values.

Ejemplos

MySQL

SELECT TO_BASE64('hello') AS encoded;  -- aGVsbG8=

PostgreSQL

SELECT encode(convert_to(payload, 'UTF8'), 'base64') AS encoded
FROM messages;

SQL Server

SELECT BASE64_ENCODE(CAST('hello' AS varbinary(10))) AS encoded;  -- aGVsbG8=

Conviene saberlo

  • MySQL's TO_BASE64 and PostgreSQL's encode(..., 'base64') both break the output into 76-character lines. Strip the newlines before putting the value in a URL, JSON field or HTTP header.
  • The output uses the standard alphabet with + and / and = padding, so it is not URL-safe. Replace + with -, / with _ and drop the padding for base64url.
  • PostgreSQL's encode and Oracle's UTL_ENCODE take bytes, so text must be converted first: encode(convert_to(str, 'UTF8'), 'base64').

Questions about TO_BASE64

Why does the Base64 output contain newlines?

MySQL and PostgreSQL follow the MIME rule of 76-character lines. Remove them with REPLACE(result, '\n', '') in MySQL or replace(result, E'\n', '') in PostgreSQL.

Is SQL Base64 URL-safe?

No. The standard alphabet includes +, / and =. For URLs, replace + with -, / with _ and drop the padding after encoding.

How do I decode Base64 back?

FROM_BASE64 in MySQL, decode(value, 'base64') in PostgreSQL, BASE64_DECODE in SQL Server 2025, then convert the bytes back to text.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.