0

I need a MySQL pattern to match a number, followed by a question mark. I need something like ... like '%[0-9]?%' but I have no idea how to create this regular expression.

http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html does not help.

Thanks!

1
  • I have a column of type text. I have to find some records in it. I have tried SELECT * FROM table WHERE column like '%[0-9]?%'; SELECT * FROM table WHERE column like '%^[0-9]?$%' and so on... Commented Apr 1, 2012 at 15:26

2 Answers 2

4

you could try this:

SELECT * FROM YourTable WHERE YourField REGEXP '[0-9]\\?'

That will return rows where YourField contains a number followed by a ? anywhere in the value.

If you want it to only match if the whole field is a number followed by a ?. I.e. 9? then you could use this regex instead:

^[0-9]\\?$
Sign up to request clarification or add additional context in comments.

2 Comments

This does not escape the ?-sign
@ДамянСтанчев your right, i totally forgot about escaping, i've corrected it.
1

I guess you're looking for something like this:

select * from table
where field rlike '[0-9]\\?'

Remember to escape the question mark. Otherwise, it will make the number optional.

Source.

1 Comment

Thank you. This works just fine. I'll also take a look at the rlike syntax.

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.