// conversion reference

SQL base64 functions

Base64 carries binary through text-only channels: TO_BASE64('SQL') is 'U1FM'. Every engine spells it differently, and two of them wrap long output across lines.

Categoria
Conversion
Returns
text or binary
In this category
23 of 39

Sintaxe

TO_BASE64(x) / FROM_BASE64(x)  -- MySQL
encode(x, 'base64') / decode(x, 'base64')  -- PostgreSQL

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

Suporte a mecanismos

MotorSupportNotas
SQL Server Different name or syntax BASE64_ENCODE/BASE64_DECODE on SQL Server 2025; the xs:base64Binary XQuery trick before
PostgreSQL Sim encode(bytea, 'base64') and decode(text, 'base64')
MySQL Sim Supported under this name.
SQLite Different name or syntax base64() is built into the sqlite3 shell, not the core library
Oracle Different name or syntax UTL_ENCODE.BASE64_ENCODE over RAW, callable from SQL

Exemplos

MySQL

SELECT TO_BASE64('SQL') AS enc, FROM_BASE64('U1FM') AS decoded;

PostgreSQL

SELECT encode('SQL'::bytea, 'base64') AS enc,
       decode('U1FM', 'base64') AS decoded;

SQL Server

SELECT BASE64_ENCODE(CAST('SQL' AS varbinary(10))) AS enc,
       CAST(BASE64_DECODE('U1FM') AS varchar(10)) AS decoded;

Bom saber

  • Output is the padded standard alphabet (+ and / with = padding). URL-safe base64 (- and _ without padding) needs REPLACE or translate() before it goes into a link.
  • PostgreSQL's encode inserts a newline every 76 characters, and MySQL's TO_BASE64 does the same; strip them when the value must be one line.
  • Decoding strictness differs: MySQL's FROM_BASE64 returns NULL on invalid input while PostgreSQL's decode raises an error.
  • SQLite's base64() is an extension compiled into the sqlite3 command-line shell; applications linking the plain library do not have it. It switches direction by argument type: a BLOB encodes, TEXT decodes.

Questions about Base64

Why is base64 output a third longer than the input?

Every 3 bytes become 4 characters, and the last group is padded to 4 with '='. A 16-byte value encodes to 24 characters, two of them padding.

Can I decode base64 to text directly?

Not in one call on most engines: decode returns bytes, so chain a cast: convert_from(decode(x, 'base64'), 'UTF8') on PostgreSQL or CONVERT(FROM_BASE64(x) USING utf8mb4) on MySQL.

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, SQLite, 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.