Re: Determine if a string is digit - Mailing list pgsql-general

From Ron St-Pierre
Subject Re: Determine if a string is digit
Date
Msg-id 3FB516DF.4090602@syscor.com
Whole thread Raw
In response to Determine if a string is digit  (Josué Maldonado <josue@lamundial.hn>)
List pgsql-general
Josué Maldonado wrote:

> Hello list,
>
> That's my question, I can't figure out a way to know if a given string
> is digit, soemthing like this:
>
> ISDIGIT("ARWA") = False
> ISDIGIT("5334")    = True
>
> If anyone know a way to get that done, I'll appreciate the help.
>
Check out the postgresql cookbook
(http://www.brasileiro.net/postgres/cookbook/). I've added the following
function:

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

-- by Ron St.Pierre (rstpierre@syscor.com)
-- 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
--
       DECLARE
               inputText ALIAS FOR $1;
               tempChar text;
               isNumeric boolean;
                      BEGIN
               isNumeric = true;
               FOR i IN 1..length(inputText) LOOP
tempChar := substr(inputText, i, 1);
                   IF tempChar ~ ''[0-9]'' THEN
                       -- do nothing
                   ELSE
                       return FALSE;
                   END IF;
               END LOOP;
                              return isNumeric;
       END;
' LANGUAGE 'plpgsql';

You need plpgsql installed for your database. Also you can use unquoted
numbers or single quoted text or numbers, double quoted values do not work.
eg
imperial=# select isdigit('234');
isdigit
---------
t
(1 row)

imperial=# select isdigit('asdf');
isdigit
---------
f
(1 row)

Ron







pgsql-general by date:

Previous
From:
Date:
Subject: Re: Conservation of OIDs
Next
From: pmorettini@earthlink.net (PhilM)
Date:
Subject: Can Postgresql run under Windows?