Categories
MySQL

Trim leading whitespace from a field in MySQL

As always, backup the database or at least the affected table before running a command that will change every record in a table as this does.

The following line will remove the leading spaces (note the parameter '\t'). For new lines use '\n' and for carriage returns use '\r'.

UPDATE members SET memberid = REPLACE(memberid,'\t','');

For more information, see http://stackoverflow.com/questions/281724/does-the-mysql-trim-function-not-trim-line-breaks-or-carriage-returns.

Categories
MySQL

Using Variables in MySQL

Finding the correct syntax for using variables in MySQL can be surprisingly difficult.
Once you know it, it's easy. Here's a start:

SET @username := 'jonesr';
SELECT id,fname,lname FROM staff WHERE username = @username;