Comments - storage/retrieval of ASCII(32)

1 year, 1 month ago Kristian Nielsen

It is because char(n) columns strip trailing spaces when selected. So chr(32), a single space, gets interpreted as the empty string. See here:

https://mariadb.com/kb/en/char/

" Trailing spaces are removed when CHAR values are retrieved"

You can set the SQL_MODE to include PAD_CHAR_TO_FULL_LENGTH to disable this behaviour and be able to select the chr(32):

MariaDB> set sql_mode = concat(@@sql_mode, ',PAD_CHAR_TO_FULL_LENGTH');
MariaDB> create table t5 (a char(1));
MariaDB> insert into t5 values ('a'), (' ');
MariaDB> select a, length(a), ascii(a) from t5;
+------+-----------+----------+
| a    | length(a) | ascii(a) |
+------+-----------+----------+
| a    |         1 |       97 |
|      |         1 |       32 |
+------+-----------+----------+
 
1 year, 1 month ago Kristian Nielsen

You can also use VARCHAR(1) as a type instead of char(1), it doesn't strip trailing spaces and thus does not require setting the SQL_MODE.

 
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.