SQL base64 decoding
The inverse of base64 encoding: bytes out. Whether those bytes are text depends on the encoding that was encoded in the first place.
- Categoria
- Strings
- Returns
binary- In this category
- 57 of 172
Sintaxe
FROM_BASE64(b64)
decode(b64, 'base64')
Each block is a separate form. The support table and the examples show which engine takes which.
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Different name or syntax | decode(b64, 'base64') returns bytea |
| MySQL | Sim | Supported under this name. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Not built in | No built-in equivalent. |
Exemplos
MySQL
SELECT FROM_BASE64('YWI=') AS s; -- ab
PostgreSQL
SELECT convert_from(decode('YWI=', 'base64'), 'utf8') AS s; -- ab
Bom saber
- MySQL's FROM_BASE64 returns NULL on invalid base64 instead of erroring. Silent NULLs can hide truncated payloads.
- PostgreSQL's decode returns bytea; convert_from(bytes, 'utf8') turns it back into text.
- Whitespace tolerance varies: MySQL skips newlines it produced, other decoders may reject them. Normalize input first.
- Round-trip encoding matters: base64 of UTF-8 bytes decodes to those bytes; interpreting them as latin1 changes accented text.
Questions about FROM_BASE64 / decode
Why does FROM_BASE64 return NULL?
MySQL returns NULL when the input is not valid base64: wrong padding, stray characters, or a truncated payload. Validate or clean the source string.
How do I get text instead of bytes?
PostgreSQL: convert_from(decode(x, 'base64'), 'utf8'). MySQL's FROM_BASE64 result already compares as a string when the bytes are valid text in the connection charset.
Is decoding base64 safe in SQL?
It is reversible encoding, not security. Decoded content is still data: treat it like any other string and keep parameterized queries for execution paths.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Custom sort order: CASE, FIELD and DECODE Dialect comparison
- NULL-safe equality: IS NOT DISTINCT FROM, <=> and IS Dialect comparison
Cheat sheets for the engines that have it: PostgreSQL, MySQL.
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.