ascii
PG 8.0+→ integerReturns the numeric Unicode code point of the first character of the string.
Signature
ascii ( string text ) → integerParameters
| Parameter | Type | Description |
|---|---|---|
| string | text | Input string; only the first character is evaluated |
Examples
Uppercase A
SELECT ascii('A');65Lowercase a
SELECT ascii('a');97Unicode euro sign
SELECT ascii('€');8364Only the first character is used
SELECT ascii('Hello');72Manual lowercase-to-uppercase via code point math
SELECT chr(ascii('a') - 32);ACalling ascii() in a loop or for each character to check whether a string is pure ASCII or contains only certain characters is slow and verbose. A single `regexp_replace` or `translate` call handles the whole string in one pass.
✓ Instead: Use `col ~ '[^\x20-\x7E]'` to detect non-ASCII characters, or `regexp_replace(col, '[^\x20-\x7E]', '', 'g')` to strip them — no loop needed.
Use `WHERE ascii(left(col, 1)) < 32` to find rows that start with invisible control characters — null bytes (0), tabs (9), carriage returns (13), escape codes — that silently break CSV exports, JSON serialization, or downstream string comparisons. Pair with `regexp_replace(col, '[\x00-\x1F]', '', 'g')` to strip them.
SELECT id, col FROM users WHERE ascii(left(col,1)) < 32;Rows with invisible leading characters