Skip to main content

ASCII

Description

Returns the ASCII code value of the first character in a string. This function only processes the first character of the string, returning only the ASCII value of the first character for multi-character strings.

Syntax

ASCII(<str>)

Parameters

ParameterDescription
<str>The string to get the ASCII code of the first character. Type: VARCHAR

Return Value

Returns INT type, representing the ASCII code value of the first character in the string.

Special cases:

  • If the parameter is NULL, returns NULL
  • If the string is empty, returns 0
  • If the first character is not an ASCII character (code value greater than 127), returns the corresponding byte value
  • For multi-byte UTF-8 characters, returns the value of the first byte

Examples

  1. Basic numeric characters
SELECT ASCII('1'), ASCII('234');
+------------+--------------+
| ASCII('1') | ASCII('234') |
+------------+--------------+
| 49 | 50 |
+------------+--------------+
  1. Letter characters
SELECT ASCII('A'), ASCII('a'), ASCII('Z');
+------------+------------+------------+
| ASCII('A') | ASCII('a') | ASCII('Z') |
+------------+------------+------------+
| 65 | 97 | 90 |
+------------+------------+------------+
  1. Empty string handling
SELECT ASCII('');
+-----------+
| ASCII('') |
+-----------+
| 0 |
+-----------+
  1. NULL value handling
SELECT ASCII(NULL);
+-------------+
| ASCII(NULL) |
+-------------+
| NULL |
+-------------+
  1. Special symbols
SELECT ASCII(' '), ASCII('!'), ASCII('@');
+------------+------------+------------+
| ASCII(' ') | ASCII('!') | ASCII('@') |
+------------+------------+------------+
| 32 | 33 | 64 |
+------------+------------+------------+
  1. Control characters
SELECT ASCII('\t'), ASCII('\n'), ASCII('\r');
+-------------+-------------+-------------+
| ASCII('\t') | ASCII('\n') | ASCII('\r') |
+-------------+-------------+-------------+
| 9 | 10 | 13 |
+-------------+-------------+-------------+
  1. Multi-character strings (returns only the first character)
SELECT ASCII('Hello'), ASCII('World123');
+----------------+------------------+
| ASCII('Hello') | ASCII('World123') |
+----------------+------------------+
| 72 | 87 |
+----------------+------------------+
  1. UTF-8 multi-byte characters
SELECT ASCII('ṭṛì'), ASCII('ḍḍumai');
+---------------+------------------+
| ASCII('ṭṛì') | ASCII('ḍḍumai') |
+---------------+------------------+
| 225 | 225 |
+---------------+------------------+
  1. Mixed numbers and characters
SELECT ASCII('9abc'), ASCII('0xyz');
+---------------+---------------+
| ASCII('9abc') | ASCII('0xyz') |
+---------------+---------------+
| 57 | 48 |
+---------------+---------------+