Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SELECT WHERE NOT A NUMBER/IS A NUMBER?

6,830 views
Skip to first unread message

Ciaran

unread,
Jan 30, 2009, 2:06:46 PM1/30/09
to
Hi Anyone know how to select only values from a row that contain a
number or that don't contain a number?
Thanks,
Ciarán

strawberry

unread,
Jan 30, 2009, 2:36:52 PM1/30/09
to

Can you provide an example?

Ciaran

unread,
Jan 30, 2009, 2:56:24 PM1/30/09
to


Sorry reading that again now, it's unusually phrased and is quite
confusing!
I'm really asking two questions:
1. how to select only records that contain a number in a varchar
column
2. how to select only records that DONT contain a number in a varchar
column

something like:
SELECT * FROM table WHERE ISNUMBER(myVarCharColumn)
SELECT * FROM table WHERE ISSTRING(myVarCharColumn)

Hopefully this makes more sense!
Ciarán

strawberry

unread,
Jan 30, 2009, 3:27:55 PM1/30/09
to

No, I meant an example of the data you're trying to work with.

Erick T. Barkhuis

unread,
Jan 30, 2009, 3:34:31 PM1/30/09
to
Ciaran:

> something like:
> SELECT * FROM table WHERE ISNUMBER(myVarCharColumn)
> SELECT * FROM table WHERE ISSTRING(myVarCharColumn)

You probably want to make use of simple regular expressions, like
^[0-9]+$ for a string that only consists of 1-n digits

Examples? See:
> <http://www.brainbell.com/tutorials/MySQL/Using_MySQL_Regular_Expressions.htm>


--
Erick

Gordon Burditt

unread,
Jan 30, 2009, 5:20:37 PM1/30/09
to
>> > Hi Anyone know how to select only values from a row that contain a
>> > number or that don't contain a number?
>> > Thanks,
>> > Ciarán
>>
>> Can you provide an example?
>
>
>Sorry reading that again now, it's unusually phrased and is quite
>confusing!
>I'm really asking two questions:
>1. how to select only records that contain a number in a varchar
>column
>2. how to select only records that DONT contain a number in a varchar
>column
>
>something like:
>SELECT * FROM table WHERE ISNUMBER(myVarCharColumn)
>SELECT * FROM table WHERE ISSTRING(myVarCharColumn)
If it's in a VARCHAR column and it's not null then it's a string.

>
>Hopefully this makes more sense!

Not really. You might make use of the regular expression matching
capabilities of MySQL but you also need to decide what you mean
by "a number".

What about stuff that contains a number and a non-number BOTH?

Classify the following inputs the way you want them:
"384 dead weasels"
"eight"
"1.2, 37.8, 22"
"-123.47e+8"
"I ate five tacos"
"I ate 5 tacos"
"I 8 5 tacos"
"19.2.34.86"
"1,348"
"+568"

ThanksButNo

unread,
Jan 31, 2009, 1:49:53 AM1/31/09
to
On Jan 30, 2:20 pm, gordonb.41...@burditt.org (Gordon Burditt) wrote:

> Classify the following inputs the way you want them:
> "384 dead weasels"
> "eight"
> "1.2, 37.8, 22"
> "-123.47e+8"
> "I ate five tacos"
> "I ate 5 tacos"
> "I 8 5 tacos"
> "19.2.34.86"
> "1,348"
> "+568"

"27.50-30.00" which happens to equal "(2.5)"

\:-\

Ciaran

unread,
Jan 31, 2009, 6:31:38 AM1/31/09
to

Oh, I just mean the most basic circumstance where one value might be
17 and the next might be "dog". In other words, the entire field is
either a number or a string.
Cheers,
Ciarán

Ciaran

unread,
Jan 31, 2009, 7:28:58 AM1/31/09
to
On Jan 30, 8:34 pm, Erick T. Barkhuis <erick.use-...@ardane.c-o-m>
wrote:
> > <http://www.brainbell.com/tutorials/MySQL/Using_MySQL_Regular_Expressi...>
>
> --
> Erick


Hey Thanks Erick, looks like this solved the problem. The my solution
is posted below for anyone interested:

Matches fields containing only numbers:
SELECT * FROM table WHERE columnName REGEXP '^[0-9]+$'

Matches fields containing only letters:
SELECT * FROM table WHERE columnName NOT REGEXP '^[0-9]+$'

I also had partial success including decimal points in the expression
like so: ^[0-9]\\.+$

Hopefully this thread helps someone else out - there's nothing much
like this out there at the moment.

Ciarán

strawberry

unread,
Jan 31, 2009, 9:21:26 AM1/31/09
to

Well then it's easy:

SELECT * FROM tester WHERE test_string > 0;


GL

unread,
Feb 15, 2009, 3:27:29 PM2/15/09
to
Ciaran a écrit :

no matter, REGEX does the job with the anchors ^ and $ :

^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$
or better :
^[-+]?[[:digit:]]*\.?[[:digit:]]+([eE][-+]?[[:digit]]+)?$

SELECT * FROM Table WHERE Field REGEX ^[-+]?[[:digit:]]*\.?[[:digit:]]+([eE][-+]?[[:digit]]+)?$

matches every floating point number (optionally with mantissa and exponent)...


ThanksButNo

unread,
Feb 16, 2009, 2:34:35 AM2/16/09
to

Ah, but will it match "(800)555-1212" -- a PHONE number??

That's a number, right?

8-D

Kees Nuyt

unread,
Feb 16, 2009, 1:22:51 PM2/16/09
to

No.

>That's a number, right?

No, that's a formatted string of digits, parentheses and hyphens,
without strict numeric meaning.
If you take care of parentheses and hyphens in the application
and store the number as 8005551212, yes, then it's a number.

I would advice to always add the international prefix to phone
numbers you store in a database.

>8-D
--
) Kees
(
c[_] As easy as 3.14159265358979323846264338327950288419716
-- [#12]

Michael Austin

unread,
Feb 16, 2009, 4:17:37 PM2/16/09
to

No - that is TEXT, CHAR or VARCHAR.

>
> 8-D

ThanksButNo

unread,
Feb 17, 2009, 12:50:19 AM2/17/09
to

Well of course it is. The whole point of the original
exercise was to read a char or varchar column and make
the determination, "Is it a number?"

E.g., for a value of "fred" the answer is No.
For a value of "19.5" the answer is Yes.

Where it gets tricky is the semantics.

Should "seventeen" be Yes or No? Depends on what you want
and what you need for your app. E.g., a check writing
application may need "numbers" such that it can print out:
"three hundred forty two dollars and five cents". Sure it's
stored in a varchar, but semantically, it's a "number".

Personally, I think "(800)555-1212" should be a number too.

Just my "too cents".

s.jm...@gmail.com

unread,
Oct 26, 2012, 4:02:59 PM10/26/12
to
0 new messages