How the ASCII() function works in Mariadb?

The ASCII() function is a string function that returns the ASCII code of the first character of a given string. The ASCII() function can be used to get the numeric value that represents a character in the ASCII encoding scheme.

Posted on

The ASCII() function is a string function that returns the ASCII code of the first character of a given string. The ASCII() function can be used to get the numeric value that represents a character in the ASCII encoding scheme. For example, the ASCII() function can be used to get the ASCII code of the letter ‘A’, which is 65.

Syntax

The syntax of the ASCII() function is as follows:

ASCII(string)

The string parameter can be any valid string expression, such as a column name, a literal value, or a function call. The string parameter is the string that you want to get the ASCII code of its first character. The string parameter cannot be NULL.

The ASCII() function returns an integer value, which is the ASCII code of the first character of the input string. If the input string is NULL, the ASCII() function returns NULL.

Examples

Here are some examples of using the ASCII() function in Mariadb:

Example 1: ASCII code of a literal string

To get the ASCII code of a literal string, you can simply pass the string as the parameter. For example:

SELECT ASCII('Hello') AS result;

This statement returns 72 as the output, which is the ASCII code of the first character of the string ‘Hello’, which is ‘H’.

Example 2: ASCII code of a column value

To get the ASCII code of a column value, you can use the column name as the parameter. For example, suppose you have a table called products that stores the name and the price of different products. You can use the ASCII() function to get the ASCII code of the first character of the name column for each product. For example:

SELECT name, price, ASCII(name) AS ascii_code
FROM products;

This statement returns the name, the price, and the ASCII code for each row in the products table. For example:

name price ascii_code
Apple 1.00 65
Banana 0.50 66
Cherry 0.75 67
Durian 2.00 68
Eggplant 1.50 69

Example 3: ASCII code of a function call

To get the ASCII code of a function call, you can use the function call as the parameter. For example, you can use the ASCII() function to get the ASCII code of the first character of the string that is returned by another string function. For example:

SELECT ASCII(UPPER('hello')) AS result;

This statement returns 72 as the output, which is the ASCII code of the first character of the string that is returned by the UPPER() function, which is ‘HELLO’.

Example 4: ASCII code of an empty string

To get the ASCII code of an empty string, you can use the empty string as the parameter. However, the ASCII() function will return NULL as the output, because the empty string has no first character. For example:

SELECT ASCII('') AS result;

This statement returns NULL as the output, because the input string is empty.

Example 5: ASCII code of a non-ASCII character

To get the ASCII code of a non-ASCII character, you can use the non-ASCII character as the parameter. However, the ASCII() function will return NULL as the output, because the non-ASCII character is not in the ASCII encoding scheme. For example:

SELECT ASCII('é') AS result;

This statement returns NULL as the output, because the input character ‘é’ is not in the ASCII encoding scheme.

Some of the functions that are related to the ASCII() function are:

  • The CHAR() function, which returns the character of a given ASCII code. The CHAR() function can be used to get the character that represents a numeric value in the ASCII encoding scheme. For example, CHAR(65) returns ‘A’ as the output, which is the character of the ASCII code 65.
  • The ORD() function, which returns the Unicode code point of the first character of a given string. The ORD() function can be used to get the numeric value that represents a character in the Unicode encoding scheme. For example, ORD('A') returns 65 as the output, which is the Unicode code point of the character ‘A’.
  • The CHR() function, which returns the character of a given Unicode code point. The CHR() function can be used to get the character that represents a numeric value in the Unicode encoding scheme. For example, CHR(65) returns ‘A’ as the output, which is the character of the Unicode code point 65.

Conclusion

The ASCII() function is a useful function that can help you get the ASCII code of the first character of a given string in Mariadb. You can use it with any valid string expression, such as a column name, a literal value, or a function call. The ASCII() function returns an integer value, which is the ASCII code of the first character of the input string. You can also use some other functions that are related to the ASCII() function, such as the CHAR(), ORD(), and CHR() functions.