ASCII¶
The ASCII function in SQL returns the ASCII value for the first character of the specified string.
Syntax¶
The syntax for the ASCII function in SQL is:
Parameters¶
single_character_text: This is the string that the ASCII value should be returned for. It should be a single character string.
Examples¶
Let's consider a few examples to understand how to use the ASCII function.
To get the ASCII value of a character:
This will return 65, which is the ASCII value for 'A'.
Please note that the ASCII function expects a single character value. If a string with more than one character is passed, it will throw an error. For instance:
This will throw an error because 'AB' contains more than one character.
You can also use the ASCII function in a SELECT statement. Consider the following table named 'Ascii':
| id | text |
|---|---|
| 1 | 'F' |
You can select the ASCII value of the 'text' column:
This will return 70, which is the ASCII value for 'F'.
The ASCII function can also take a string directly:
This will return 97, which is the ASCII value for 'a'.
If a non-ASCII character is passed to the function, it will throw an error. For instance:
This will throw an error because 'ㄱ' is not an ASCII character.
If no argument is passed to the ASCII function, it will also throw an error:
This will throw an error because the ASCII function expects one argument.
Remember, the ASCII function expects a single character. If the column value contains more than one character, it will throw an error:
This will throw an error because 'Foo' contains more than one character.