SQL CHR / CHAR function
The inverse of ASCII: numbers become characters, which is how SQL builds newlines, tabs and symbols without literals that editors mangle.
- 类别
- Strings
- Returns
string- In this category
- 50 of 172
语法
CHR(n)
CHAR(n [, n ...])
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | 是 | Supported under this name. |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | 是 | CHAR() accepts a comma-separated list and returns binary unless you add USING |
| SQLite | 是 | spelled char(), also takes a list |
| Oracle | 是 | Supported under this name. |
示例
SQL Server
SELECT 'line1' + CHAR(13) + CHAR(10) + 'line2' AS two_lines;
PostgreSQL
SELECT CHR(233) AS e_acute; -- é
SQLite
SELECT 'line1' || char(10) || 'line2' AS two_lines;
值得了解
- Names split: CHAR on SQL Server, MySQL and SQLite; CHR on PostgreSQL and Oracle. MySQL's CHAR(65, 66) takes a list; SQLite's char() too.
- Oracle: embedded CHR(0) acts like an end-of-string marker in many interfaces; the value truncates silently in binds and clients.
- SQL Server CHAR(n) works on the varchar codepage: CHAR(233) may be é on Latin1 and something else elsewhere. NCHAR(n) is the Unicode call; values above 65535 need a supplementary-character (_SC) collation, otherwise NCHAR returns NULL.
- MySQL's CHAR() takes byte values and returns a binary string. CHAR(n USING utf8mb4) reads n as UTF-8 bytes, so an NBSP is CHAR(0xC2A0 USING utf8mb4), not CHAR(160 USING utf8mb4).
Questions about CHR / CHAR
Is it CHR or CHAR?
Both: SQL Server, MySQL and SQLite call it CHAR, PostgreSQL and Oracle call it CHR. Same job: integer code point in, one-character string out.
Why does CHAR(233) show a different character on SQL Server?
CHAR returns varchar, interpreted in the column's or database's codepage. NCHAR(233) returns nvarchar and always means Unicode code point 233.
Is CHR(0) safe?
On Oracle it behaves as a string terminator in many contexts, silently truncating. On SQL Server and MySQL it embeds, but clients and drivers may still cut there.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- CHAR versus VARCHAR semantics in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Find rows containing control characters Snippet
- Remove all whitespace from a string in SQL Snippet
- Remove non-printable and control characters in SQL Snippet
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.