How to test a variable to determine if it is all digits.

Posted on 2007-07-24
Last Modified: 2008-07-03
Say i've got a variable v_myvariable which is varchar(20) and equal to '01234tt567'.

In pl/pgsql, I would like to see a test that will determine if the variable is all digits.

100 points for each right answer.

Question by:gmanpert
    LVL 10

    Accepted Solution

    CREATE OR REPLACE FUNCTION isdigit(text) RETURNS boolean as '

    -- by Ron St.Pierre (
    -- licensed under the GPL
    -- determines whether or not a value is numeric
    -- required fields: string or number, single quoted
    -- returns: true - if input is numeric, false otherwise
    inputText ALIAS FOR $1;
    tempChar text;
    isNumeric boolean;
    isNumeric = true;
    FOR i IN 1..length(inputText) LOOP
    tempChar := substr(inputText, i, 1);
    IF tempChar ~ ''[0-9]'' THEN
    -- do nothing
    return FALSE;
    END IF;
    return isNumeric;
    ' LANGUAGE 'plpgsql';


    Author Comment

    Yeah, and I figured out these work as well...

    select '1234GG567' ~ '^[0-9]+$';
    select '1234567' ~ '^[0-9]+$';

    if v_var_data ~ '^[0-9]{9}$'
      ... good ..
      .. bad ..
     end if;

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: ( This requires some add-o…
    Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
    Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now