Re: Determine if a string is digit
| От | Ron St-Pierre | 
|---|---|
| Тема | Re: Determine if a string is digit | 
| Дата | |
| Msg-id | 3FB516DF.4090602@syscor.com обсуждение исходный текст | 
| Ответ на | Determine if a string is digit (Josué Maldonado <josue@lamundial.hn>) | 
| Список | 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 по дате отправления: