Thread: CHAR to INT

CHAR to INT

From
"Martín Marqués"
Date:
I'm trying to make a PL function to pass from an alphanumeric string
to a numeric one changing the chars in the string to it's ORD (the
inverse of the chr function) and I'm stumbling with two problems:

1) How to know if the CHAR is numeric or not.
2) If it's not numeric I want to get it's ord, but I can't find
something like that in PG.

Any ideas?

--
Martín Marqués
select 'martin.marques' || '@' || 'gmail.com'
DBA, Programador, Administrador

Re: CHAR to INT

From
"Pavel Stehule"
Date:
Hello

2008/7/12 Martín Marqués <martin.marques@gmail.com>:
> I'm trying to make a PL function to pass from an alphanumeric string
> to a numeric one changing the chars in the string to it's ORD (the
> inverse of the chr function) and I'm stumbling with two problems:
>
> 1) How to know if the CHAR is numeric or not.
> 2) If it's not numeric I want to get it's ord, but I can't find
> something like that in PG.
>
postgres=# select '1' between '0' and '9';?column?
----------t
(1 row)

postgres=# select 'a' between '0' and '9';?column?
----------f
(1 row)

postgres=# select ascii('7') - ascii('0');?column?
----------       7
(1 row)


Regards
Pavel Stehule

> Any ideas?
>
> --
> Martín Marqués
> select 'martin.marques' || '@' || 'gmail.com'
> DBA, Programador, Administrador
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

Re: CHAR to INT

From
"Martín Marqués"
Date:
2008/7/12 Pavel Stehule <pavel.stehule@gmail.com>:
> Hello
>
> 2008/7/12 Martín Marqués <martin.marques@gmail.com>:
>> I'm trying to make a PL function to pass from an alphanumeric string
>> to a numeric one changing the chars in the string to it's ORD (the
>> inverse of the chr function) and I'm stumbling with two problems:
>>
>> 1) How to know if the CHAR is numeric or not.
>> 2) If it's not numeric I want to get it's ord, but I can't find
>> something like that in PG.
>>
> postgres=# select '1' between '0' and '9';
>  ?column?
> ----------
>  t
> (1 row)
>
> postgres=# select 'a' between '0' and '9';
>  ?column?
> ----------
>  f
> (1 row)
>
> postgres=# select ascii('7') - ascii('0');
>  ?column?
> ----------
>        7
> (1 row)
>

Saw the ascii function when I had already sent the mail. :-(

Thanks any way.

--
Martín Marqués
select 'martin.marques' || '@' || 'gmail.com'
DBA, Programador, Administrador