Обсуждение: Determine if a string is digit
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.
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.
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.
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