7

I'm trying to return only those rows which colA and colB do not contain a number or a whitespace

Here is my code so far.

SELECT * FROM table_name WHERE colA REGEXP '[^0-9^\W]' AND colB REGEXP '[^0-9^\W]'

given the dataset

   colA        colB
---------- ----------
     test |  testB
      33  |  text    <- this is not returned
     blah |  0123A   <- this is returned

I am assuming my issue is with my regexp... any help please?

5 Answers 5

9

Well your expression does not work because: 33 is both numbers and you're looking for any non decimal, non whitespace character. so it doesn't include that row in result.

Try:

SELECT * FROM table_name WHERE colA NOT REGEXP '[0-9[:space:]]' AND colB NOT REGEXP '[0-9[:space:]]'

ETA: Yea forgot that \whatever does not work

Sign up to request clarification or add additional context in comments.

4 Comments

Ok so how would I then filter out anything that contains a number.. and 'just a space'. Basically I have a data import that contains a lot of invalid values. (names that contain numbers, or are missing completely) I only want to return valid names. so IF val DOES NOT CONTAIN A NUMBER AND IS NOT BLANK (OR JUST WHITESPACE) note the columns are 'NOT NULL' flagged. So I cannot check for a null value.
Note: I re-evaluated the scope, you were correct with this answer.
Ok, so WHERE colA LIKE "" worked for removing the blank cells (which are not null)
This work fine WHERE colA not REGEXP '^[0-9]+';
2

MySQL doesn't support the \whatever type escapes for the most part. If you want whitespace, you need to use [[:space:]], which is POSIX syntax. Details on the regex man page here.

Comments

1

id suggest to use negate instead: not regexp '[0-9[:space:]]'

SELECT * FROM table_name WHERE colA NOT REGEXP '[0-9[:space:]]' AND colB NOT REGEXP '[0-9[:space:]]'

Comments

1

Another regex you can try is the following: regexp '[^0-9\s]'

Comments

0

Not tested but give it a try

regexp '^[^0-9 ]+$'

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.