How the CHR() function works in Mariadb?

The CHR() function is a string function that returns a single character based on its numeric code.

Posted on

The CHR() function is a string function that returns a single character based on its numeric code. The CHR() function is equivalent to the CHAR() function with a single argument, and can be used to create special characters, such as control characters, escape characters, or non-printable characters.

Syntax

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

CHR(N [USING charset_name])

Where:

  • N is an integer value that represents the numeric code of the character to be returned. The numeric code is based on the ASCII or Unicode standard, depending on the character set used.
  • charset_name is an optional argument that specifies the name of the character set to use. If not specified, the default character set of the connection is used.

The return type of the function is a string value of length one.

Examples

Example 1: Creating a character from an ASCII code

In this example, we use the CHR() function to create a character from an ASCII code. We use the SELECT statement to display the result.

SELECT CHR(65) AS character;

The output is:

+----------+
| character|
+----------+
| A        |
+----------+

Example 2: Creating a character from a Unicode code

In this example, we use the CHR() function to create a character from a Unicode code. We use the USING argument to specify the utf8mb4 character set. We use the SELECT statement to display the result.

SELECT CHR(128512 USING utf8mb4) AS character;

The output is:

+----------+
| character|
+----------+
| 😀       |
+----------+

Example 3: Creating a control character

In this example, we use the CHR() function to create a control character. We use the ASCII code 9, which represents the horizontal tab character. We use the SELECT statement to display the result.

SELECT CONCAT('Hello', CHR(9), 'World') AS string;

The output is:

+--------------+
| string       |
+--------------+
| Hello	World |
+--------------+

Some other functions that are related to the CHR() function are:

  • ASCII(): Returns the ASCII code of the first character of a string. The syntax is ASCII(string).
  • UNICODE(): Returns the Unicode code of the first character of a string. The syntax is UNICODE(string).
  • ORD(): Returns the numeric code of the leftmost character of a string. The syntax is ORD(string).
  • CHAR(): Returns a string value composed of one or more characters based on their numeric codes. The syntax is CHAR(N1[, N2, ...] [USING charset_name]).

For example, you can use the ASCII() function to get the ASCII code of the first character of a string, which is the opposite of the CHR() function:

SELECT ASCII('A') AS code;

The output is:

+------+
| code |
+------+
| 65   |
+------+

Conclusion

The CHR() function is a useful function to create a single character based on its numeric code. The CHR() function is equivalent to the CHAR() function with a single argument, and takes an integer value as an argument, and returns a string value of length one that contains the corresponding character. The numeric code is based on the ASCII or Unicode standard, depending on the character set used. The CHR() function can also take an optional argument that specifies the name of the character set to use. The CHR() function can be used to create special characters, such as control characters, escape characters, or non-printable characters. The CHR() function can be combined with other functions to perform various string operations and analyses.