// strings reference

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.

// 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.