detox1978
asked on
MySQL: WHERE field only contains letters, numbers, single spaces and no leading/trailing space
Hi All,
As the title suggests, I am looking to create a SELECT query that has a WHERE clause to display only companies that only contains letters, numbers, single spaces and no leading/trailing space
SELECT * FROM MyTable WHERE company NOT LIKE '% %' etc...
Many thanks
D
As the title suggests, I am looking to create a SELECT query that has a WHERE clause to display only companies that only contains letters, numbers, single spaces and no leading/trailing space
SELECT * FROM MyTable WHERE company NOT LIKE '% %' etc...
Many thanks
D
ASKER
Any thoughts on the only letters and numbers?
Obviously, No Trailing spaces would be.
Also, I need it to be a single query
Obviously, No Trailing spaces would be.
SELECT * FROM MyTable WHERE company NOT LIKE '% '
Also, I need it to be a single query
ASKER
I found this online.
So I need to add the single space and no leading/trailing space to it
SELECT * FROM MyTable WHERE Company REGEXP '^[A-Za-z0-9]+$'
So I need to add the single space and no leading/trailing space to it
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Regular expression engines differ a little from each other; I think you're headed in the right direction with that pattern. I would try what you have both with and without the plus sign. Be aware that it will match a single blank, or multiple blanks, too. You may want to do a "down-select" into a temporary table so you can use more than one REGEX to filter the data.
ASKER
HI Ray,
I found that on the web, I dont really understand it.
I found that on the web, I dont really understand it.
detox, you are almost there but that solution miss the leading spaces. For that I added the following:
RegExp --> Regular Expression - are the valid expressions that you want to find in a string. You can also NEGATE and then means that you don't want the strings with the presented expressions (NOT REGEXP).
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$' AND LEFT(column, 1)<>' '
RegExp --> Regular Expression - are the valid expressions that you want to find in a string. You can also NEGATE and then means that you don't want the strings with the presented expressions (NOT REGEXP).
ASKER
Regex I found is the closest.
Open in new window