-2

https://i.stack.imgur.com/TrOtF.png

Please see picture above and explain me why it says in CHAR that trailing spaces are removed and why VARCHAR says that does not remove trailing spaces.

I thought VARCHAR was the one that removed the whitespaces in order to storage only the required bytes and not CHAR. Thanks.

eberteo
  • 23
  • 5

1 Answers1

1

There is a limitation is Mysql that it only assign certain length to CHAR variable which is known as PAD_CHAR_TO_FULL_LENGTH. This PAD_CHAR_TO_FULL_LENGTH limitation doesnot implies on VARCHAR Variable. Due to this limitation, When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled

And to enable PAD_CHAR_TO_FULL_LENGTH SQL mode, execute a plain old SQL command along the following line

 SET SESSION sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';

Now you should be aware that why varchar donot trail white spaces. It is because there is no such PAD_CHAR_TO_FULL_LENGTH limitation on varchar variable.