1

Recently I started to learn MySQL. In the book, this sentence was written.

mysql> SELECT 'abcdefg', CHAR(97);
+---------+--------------------+
| abcdefg | CHAR(97)           |
+---------+--------------------+
| abcdefg | a                  |
+---------+--------------------+
1 row in set (0.00 sec)

but, I got something different, Hexadecimal:

mysql> SELECT 'abcdefg', CHAR(97);
+---------+--------------------+
| abcdefg | CHAR(97)           |
+---------+--------------------+
| abcdefg | 0x61               |
+---------+--------------------+
1 row in set (0.00 sec)

I want to know the way to get ascii code value.
MySQL version is 8.

D-Shih
  • 44,943
  • 6
  • 31
  • 51
신유철
  • 61
  • 1
  • That is only an other way to represent 'a(bcdefg)' in the command line console,depending on a setting. 0x61 = 'a' – Joop Eggen Apr 21 '22 at 05:56

1 Answers1

2

I think the problem is the setting --binary-as-hex is enabled, which might enable as a default of MySQL 8.0.19

As of MySQL 8.0.19, when mysql operates in interactive mode, this option is enabled by default

We can run the status or \s command includes this line when the option is enabled we will see

Binary data as: Hexadecimal

To write a binary string expression so that it displays as a character string regardless of whether --binary-as-hex is enabled, use these techniques:

We can try to use the USING charset clause in CHAR function to get your expect result.

SELECT CHAR(97 USING utf8mb4);

You can try to use CHARSET to check the character set of the argument

SELECT 'abcdefg', CHAR(97),CHARSET(CHAR(97))
abcdefg CHAR(97) CHARSET(CHAR(97))
abcdefg a binary

sqlfiddle

D-Shih
  • 44,943
  • 6
  • 31
  • 51