SQL Unicode string literals
Mark a literal as Unicode text or risk the classic result: accented characters silently become ? on the way into varchar columns.
- Category
- Strings
- Returns
string- In this category
- 80 of 172
Syntax
N'café'
_utf8mb4'café'
'café'
Each block is a separate form. The support table and the examples show which engine takes which.
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Yes | Supported under this name. |
| PostgreSQL | Yes | Supported under this name. |
| MySQL | Yes | Supported under this name. |
| SQLite | Yes | Supported under this name. |
| Oracle | Yes | Supported under this name. |
Examples
SQL Server
SELECT N'café' AS good, 'café' AS risky;
MySQL
SELECT _utf8mb4'é' AS ch;
PostgreSQL
SELECT 'café' AS plain; -- no prefix in a UTF8 database
Good to know
- SQL Server: N'café' is nvarchar; the plain 'café' literal is varchar and non-representable characters become ? under a non-Unicode collation.
- MySQL: literals take the connection charset; introducers like _utf8mb4'...' pin it per literal. The connection charset is usually the real lever.
- PostgreSQL: literals use the database encoding, UTF-8 on nearly every install, so no prefix is needed. U&'d\0061ta' writes characters by code point.
- Oracle: N'...' is for NCHAR types; in an AL32UTF8 database a plain literal already carries Unicode; the prefix matters for national-charset columns.
Questions about Unicode literals (N'...')
Why did my accented text become question marks?
The literal or the column was non-Unicode: a plain varchar literal under a Latin collation cannot hold é and stores ?. Use N'' literals and nvarchar columns on SQL Server.
Do I need N'' in PostgreSQL or MySQL?
PostgreSQL: no. Text uses the database encoding. MySQL: usually no. The connection charset decides; introducers are for pinning a specific charset per literal.
What is U&'...' in PostgreSQL?
Unicode escape syntax: U&'d\0061ta' inserts characters by hex code, with \ as the default escape, useful when source files must stay ASCII.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
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.