Remove start and end space SQL / phpMyAdmin

Some of my data has spaces at the start or end and this is causing my SQL queries not to give the correct results.

Below is my query...

$query_rs_media_new_cat_filter = "SELECT DISTINCT category
								  FROM media_new
								  WHERE type = 'Ink' OR type='Misc'
								  ORDER BY category ASC";

Open in new window

How do I remove the space from the "category" output column?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


pls try

$query_rs_media_new_cat_filter = "SELECT DISTINCT TRIM(category) As Category
								  FROM media_new
								  WHERE type = 'Ink' OR type='Misc'
								  ORDER BY TRIM(category) ASC";

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Going forward, it's considered "best practice" to sanitize and normalize all external inputs to your PHP scripts.  PHP has a trim() function, too, so you can avoid putting " Ink" into the database when what you really want is "Ink" without the leading space.

If you already have " Ink" or " Ink " with some white space around them, you might want to consider running a query to read and update all of the rows in a way that corrects the unwanted whitespace.  A WHERE clause that might be useful would look something like this:

WHERE type LIKE '%Ink%'

MySQL can use the percent sign as a wild-card operator to match anything or nothing in the WHERE clause.  The exact effect of wild-card matching is dependent on the contents of the database, so you would really want to back up the tables before you ran such a query, and be sure you weren't accidentally matching something like "think" when you ran the update.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I will go along Rays comment, and suggest you make sure the user data is "trimmed" before saving to the db
once that is done, you could clean the existing data once (update yourtable set category = trim(category); )  and avoid doing the trim in the select anyhow.
because then, with a index on category column, you will have a faster performance for that query (as only the index data needs to be read an not the table itself)

next step would be normalization: create a category table (id + name), and store in your data table only the ID value instead of the actual names ... will help you later at plenty of other scenarios
petewinterAuthor Commented:
Many thanks for the help
Guy Hengel [angelIII / a3]Billing EngineerCommented:
yes, retrieving from 2 tables can be faster... parallel reads
apart from the fact that small lookup tables are usually in memory anyhow (as hot tables)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.