Determine if a string is digit

Started by Josué Maldonadoover 22 years ago4 messagesgeneral
Jump to latest
#1Josué Maldonado
josue@lamundial.hn

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.

--
Josu� Maldonado.

#2Manuel Sugawara
masm@fciencias.unam.mx
In reply to: Josué Maldonado (#1)
Re: Determine if a string is digit

Josué Maldonado <josue@lamundial.hn> writes:

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.

create function isdigit(text) returns boolean as '
select $1 ~ ''^(-)?[0-9]+$'' as result
' language sql;

masm=# select isdigit('ARWA');
isdigit
---------
f
(1 row)

masm=# select isdigit('5334');
isdigit
---------
t
(1 row)

Regards,
Manuel.

#3Josué Maldonado
josue@lamundial.hn
In reply to: Manuel Sugawara (#2)
Re: Determine if a string is digit

Thanks Manuel,

It works nice!

Manuel Sugawara wrote:

Josu� Maldonado <josue@lamundial.hn> writes:

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.

create function isdigit(text) returns boolean as '
select $1 ~ ''^(-)?[0-9]+$'' as result
' language sql;

masm=# select isdigit('ARWA');
isdigit
---------
f
(1 row)

masm=# select isdigit('5334');
isdigit
---------
t
(1 row)

Regards,
Manuel.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

--
Josu� Maldonado.

#4Ron St-Pierre
rstpierre@syscor.com
In reply to: Josué Maldonado (#3)
Re: Determine if a string is digit

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