Re: Determine if a string is digit

From: Ron St-Pierre <rstpierre(at)syscor(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Determine if a string is digit
Date: 2003-11-14 17:54:39
Message-ID: 3FB516DF.4090602@syscor.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)syscor(dot)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

Browse pgsql-general by date

  From Date Subject
Next Message btober 2003-11-14 17:55:16 Re: Conservation of OIDs
Previous Message Rajesh Kumar Mallah 2003-11-14 17:24:22 DOMAIN usability