D
D
Dmitry Sergeev2012-02-10 13:14:52
MySQL
Dmitry Sergeev, 2012-02-10 13:14:52

mysql. How to select rows in a field that contains only numbers?

Let's say the table has a first_letter field, which can be either a number or a letter. How to select only those lines for which first_letter=number?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
dali, 2012-02-10
@dali

firstr_letter only one character?
test the following constructs for speed:
- WHERE first_letter IN (1,2,3,4,5,6,7,8,9,0)
- WHERE first_letter REGEXP '[0-9]'

M
MaxUp, 2012-02-10
@MaxUp

something like this:
SELECT * FROM ... WHERE first_letter REGEXP '^[0-9]+$';
and if there is only one character, then you can simply
SELECT * FROM ... WHERE first_letter REGEXP '^[0-9]';

S
Stdit, 2012-02-10
@Stdit

Regular expressions are not the best idea (see EXPLAIN).
select * from t where str<='9'
select * from t where str in ('0','1','2','3','4','5','6','7' ,'8','9')

D
deadkrolik, 2012-02-11
@deadkrolik

Oh, you wrote.
SELECT * FROM x WHERE IS_NUMERIC(first_letter)

A
Andrey Kroshkin, 2017-03-18
@andrium

Does MySQL have the IS_NUMERIC.. function?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question