1

Let's suppose I have a table like this...

id | value
----------
1  | 4
2  | a8
3  | 13
4  | a2
5  | 7

So some numbers in the value have an "a" in front of them and others don't.

Now let's say I wanted to pull out anything higher than 6 - including any number with an "a" in front of it.

So a query that looked something like this (inserting some PHP that I know wouldn't work in SQL)

SELECT * FROM table WHERE str_replace("a","",value)>6;

The expected results should be...

id | value
----------
2  | a8
3  | 13
5  | 7

Please note that I don't want to actually get rid of the "a"s in the SQL table. The table should remain untouched. It's okay if the result back doesn't have the "a" in it, though.

1 Answer 1

1

Basically you have to:

  1. Clean the data
  2. Cast to int (using + 0 as a shortcut)
  3. Use the result to filter

Try this:

SELECT id, value FROM table
WHERE REPLACE(value, "a", "") + 0 > 6

If you also wanted to have the "cleaned" field in your results you would have to add the REPLACE into your select statement too:

SELECT id, REPLACE(value, "a", "") + 0 value FROM table
WHERE REPLACE(value, "a", "") + 0 > 6
Sign up to request clarification or add additional context in comments.

1 Comment

This is it! Thanks so much! (will mark as answer as soon as I'm allowed)

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.