storage/retrieval of ASCII(32)

problems storing/retrieving (ascii 32) value, it can be stored, but cannot be retrieved correctly.

for example :we have table x in which there is a column col( char(1) type) , when we are inserting chr(32) in col , t is inserting successfully but the time of retrieving it is giving output 0.

select ASCII(col) from x;

output is 0 instated of 32.

Answer Answered by Kristian Nielsen in this comment.

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 |
+------+-----------+----------+

Comments

Comments loading...
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.