// strings reference

SQL CHR code point

Returns the character for a numeric code: a code point in some engines, a byte value in others.

Categoría
Strings
Returns
string
In this category
128 of 172

Sintaxis

CHR(code)
SQL Server and MySQL: CHAR(code)
SQLite: char(code)

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

Soporte del motor

MotorSupportNotas
SQL Server Different name or syntax CHAR(n) uses the collation's code page and returns NULL above 255; NCHAR(n) takes code points.
PostgreSQL Sí chr(n) takes a code point in UTF8 databases; chr(0) is an error.
MySQL Different name or syntax CHAR(n) treats n as bytes; CHAR(n USING utf32) takes a code point.
SQLite Different name or syntax char(n, ...) takes code points and accepts several.
Oracle Sí CHR(n) takes a byte value in the database character set; NCHR(n) takes a code point.

Ejemplos

PostgreSQL

SELECT CHR(65) AS letter;  -- A

PostgreSQL

SELECT split_part(note, chr(10), 1) AS first_line
FROM logs;

SQL Server

SELECT CHAR(65) + CHAR(66) + CHAR(67) AS letters;  -- ABC

Conviene saberlo

  • Engines disagree on what the number means: a code point (PostgreSQL in UTF8, SQLite, SQL Server's NCHAR), a code-page byte (SQL Server's CHAR) or encoded bytes (MySQL's CHAR, Oracle's CHR). Below 128 they all agree.
  • The line-break codes: 10 is newline, 13 carriage return, 9 tab. Windows line endings are CHR(13) || CHR(10).
  • PostgreSQL cannot store the NUL character in text, so chr(0) raises an error; SQL Server, MySQL and SQLite accept a NUL character.

Questions about CHR

What is CHR(10) used for?

Newlines. Splitting or replacing on CHR(10) (CHAR(10) in SQL Server and MySQL) is how you handle multi-line text stored in a column.

Is CHR the same as CHAR in SQL Server?

For codes 0 to 127, yes. Above that SQL Server's CHAR follows the collation's code page and returns NULL past 255; use NCHAR(n) for Unicode code points.

How do I convert a character back to its code?

ASCII(ch) in most engines for 0 to 127; UNICODE(ch) in SQL Server and SQLite for any code point; ascii(ch) in PostgreSQL, which returns the code point in UTF8 databases.

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.