Faqts : Business : Databases : MySQL : Language and Syntax : Field Types : Varchar

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 14 people (43%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

How can I select only positive numbers from a varchar field?
How can I select only numbers from a varchar field?

Jan 25th, 2000 06:37
Nathan Wallace, Sinisa Milivojevic, Stefano Ricci


Assume you have a table like this:

    CREATE TABLE foo (
        a VARCHAR(20)
    )

    INSERT INTO foo (a) VALUES('hello');
    INSERT INTO foo (a) VALUES('55');

To select only the rows that contain a number greater than zero do:

    SELECT * FROM foo WHERE a 0 > 0;

To get rows with a = positive, negative or even zero try this:

    SELECT * FROM foo WHERE a 0<>0 or a regexp "^0(.0 )?$"