20

I would like to run a query on a MySQL database table to return all the rows where the column data has white space. e.g.:

Usernames
andy davies
peter
geoff smith
steve
bob paul

The query would only return 'andy davies', 'geoff smith', 'bob paul' and ignore 'peter' and 'steve'

Any ideas?

4 Answers 4

64
SELECT ...
   ...
  WHERE username LIKE '% %'
Sign up to request clarification or add additional context in comments.

1 Comment

This didn't work for me, even though there were a couple of rows containing a space.
9

INSTR() returns the position of the specified substring in the given string. Use it in the WHERE conditional.

SELECT * FROM table WHERE INSTR(`username`, ' ') > 0

Comments

1

An idea:

SELECT names FROM tbl_name WHERE names LIKE "% %";

Comments

1

This version checks for all white space including tabs and new lines (it assumes the column is username):

SELECT
  username,
  REPLACE(REPLACE(REPLACE(username, ' ', ''), '\t', ''), '\n', '') AS username_clean
FROM [your_table]
HAVING username <> username_clean;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.