MYSQL Whitespace Issues

MYSQL Whitespace Issues

With mySQL your data output could be suspicious. One such culprit could be whitespace; which is easy to miss at first glance.

However, if you are stuck with trailing whitespace in a VARCHAR column, you can remove it through a two step process:

1) alter column type to char
2) alter column type back to varchar.

Another method is to use trim() or ltrim() function with mySQL or phpMyAdmin.

Here is a query which trism whitespace for all entries in a column for which the id >0.

UPDATE tablename set columnname = rtrim(columnname) WHERE id >0;

Yet, another method is to trim whuitespace with PHP. You can add the trim() function to post variables that are inserted into a database so that whitespace is removed.

Example query:
$var = trim($_POST['name']);

$command = "INSERT into tablename VALUES(NULL, '$var')";