Skip to content
Advertisement

Query to check if a certain row has 2 words

How to return rows where a column has 2 words (that is, strings separated by a space) in it?

It must be purely using SQL.

SELECT * FROM table WHERE name (has 2 strings in it);

Advertisement

Answer

I dont know the names when querying. Its a big dataset. I only have to check if the name contains a spacebar basically (from a comment).

If you want to distinguish names that have two parts from one-part and three-plus-part names, you can use regular expression:

SELECT * FROM my_table WHERE name REGEXP '^[^ ]+[ ]+[^ ]+$'

This regular expression matches when the entire string consists of two non-empty parts containing no spaces, with one or more space separating them.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement