Trailing spaces in MySQL

November 27, 2006
Author
Peter Zaitsev
Share this Post:

In the past life was easy in MySQL. Both CHAR and VARCHAR types meant the same, only being difference in the sense of fixed or dynamic row length used. Trailing spaces were removed in both cases.

With MySQL 5.0 however things changed so now VARCHAR keeps trailing spaces while CHAR columns do not any more. Well in reality CHAR columns are padded to full length with spaces but it is invisible as those trailing spaces are removed upon retrieval. This is something you need to watch both upgrading to MySQL 5.0 as well as designing your applications – you should keep into account if you mind trailing spaces stored choosing VARCHAR vs CHAR in addition to fixed length vs dynamic level rows and space spent for column size counter.

There is more fun stuff with trailing spaces. When comparison is done trailing spaces are always removed, even if VARCHAR column is used which is pretty counterintuitive. So “a “=”a”=”a ” for all textual column types – CHAR, VARCHAR, TEXT. BLOB is exception it will preserve trailing spaces and use them in comparison.

0 0 votes
Article Rating
Subscribe
Notify of
guest

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Martijn Tonies
19 years ago

Are you sure that CHARs has spaces that are invisible? Cause that would be wrong according to the SQL Standard. VARCHARs should be trimmed.

Jay Pipes
19 years ago

Unless, of course, you compare using BINARY:

mysql> SELECT “a ” = “a”;
+————+
| “a ” = “a” |
+————+
| 1 |
+————+
1 row in set (0.00 sec)

mysql> SELECT BINARY “a ” = BINARY “a”;
+————————–+
| BINARY “a ” = BINARY “a” |
+————————–+
| 0 |
+————————–+
1 row in set (0.00 sec)

I think, however, a best practice would be to have the application strip trailing spaces before insertion. I can’t think of a good reason *not* to do so…

Jay

Alex
Alex
16 years ago

Wow! “a” = “a “… I did not know about this. Thanks for this information!

Mike
Mike
16 years ago

I also did not know this, thanks

richlv
richlv
13 years ago

“I think, however, a best practice would be to have the application strip trailing spaces before insertion. I can’t think of a good reason *not* to do so…”

IPMI sensors can have trailing space in some implementations. storing the sensor in the db means the space is actually meaningful (sensor names are normal, non-binary strings)

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved