ASCII

Return the Unicode code point of the first character of the input string.

Category: stringReturns: INTEGERDialect: Standard

Syntax

ASCII(str)

Description

## Overview Returns the numeric Unicode code point of the first character of the input string. For ASCII characters (code points 0 to 127), the result is the classic ASCII code. For characters outside the ASCII range, the result is the full Unicode code point (which may be larger than 127 and up to 1,114,111 for supplementary planes). ASCII is the inverse of CHR/CHAR: ASCII(CHR(65)) returns 65. Typical uses include classifying input data (is the first character a letter, a digit, a control character?) and detecting non-ASCII content quickly without scanning the entire string. ## Behavior - Returns NULL when the input is NULL. - Returns 0 when the input is an empty string. - Returns the Unicode code point of the first code point in the string. For characters in the Basic Multilingual Plane this matches their UTF-16 code unit; for characters in supplementary planes, the full code point (up to 0x10FFFF) is returned. - Only the first character is evaluated; the rest of the string is ignored without error. - Operates on Unicode code points, not bytes: the first UTF-8 byte alone is not decoded as a number. ## Compatibility - Matches the common SQL convention for ASCII where non-ASCII characters return their Unicode code point. - For strictly ASCII-only data the result is always in [0, 127].

Parameters

NameTypeDescription
strSpecifies the input string. Only the first character is inspected; remaining characters are ignored.

Examples

-- Code point of a letter
SELECT ASCII('A');  -- 65
-- Code point of a digit character
SELECT ASCII('0');  -- 48
-- Only the first character is evaluated
SELECT ASCII('Hello');  -- 72
-- Lowercase letter
SELECT ASCII('a');  -- 97
-- Empty string returns 0
SELECT ASCII('');  -- 0
-- NULL propagates
SELECT ASCII(CAST(NULL AS VARCHAR));  -- NULL

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →