pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedcreate function atof?
5+ messages / 5 participants
[nested] [flat]
* create function atof?
@ 2004-02-20 14:01 Mark Tabash <mark.tabash@novacolor.ca>
2004-06-06 02:16 ` Re: create function atof? Yasir Malik <ymalik@cs.stevens-tech.edu>
0 siblings, 1 reply; 5+ messages in thread
From: Mark Tabash @ 2004-02-20 14:01 UTC (permalink / raw)
To: pgsql-sql
Hello,
Is it possible to create a database function that mimics the C function atof?
I'm guessing it should look something like this:
create function atof(varchar) returns float
as '??????'
language ????
returns null on null input;
Thanks,
Mark
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: create function atof?
2004-02-20 14:01 create function atof? Mark Tabash <mark.tabash@novacolor.ca>
@ 2004-06-06 02:16 ` Yasir Malik <ymalik@cs.stevens-tech.edu>
0 siblings, 0 replies; 5+ messages in thread
From: Yasir Malik @ 2004-06-06 02:16 UTC (permalink / raw)
To: Mark Tabash <mark.tabash@novacolor.ca>; +Cc: pgsql-sql
> Is it possible to create a database function that mimics the C function atof?
> I'm guessing it should look something like this:
You can do this to convert a string to a float:
select '3.14'::float + 1;
?column?
----------
4.14
(1 row)
Is that what you want?
Yasir
^ permalink raw reply [nested|flat] 5+ messages in thread
* create function atof?
@ 2004-02-20 14:04 mark <postgresql@novacolor.ca>
2004-02-20 14:24 ` Re: create function atof? Achilleus Mantzios <achill@matrix.gatewaynet.com>
2004-02-20 14:54 ` Re: create function atof? Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 5+ messages in thread
From: mark @ 2004-02-20 14:04 UTC (permalink / raw)
To: pgsql-sql
Hello,
Is it possible to create a database function that mimics the C function atof?
I'm guessing it should look something like this:
create function atof(varchar) returns float
as '??????'
language ????
returns null on null input;
Thanks,
Mark
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: create function atof?
2004-02-20 14:04 create function atof? mark <postgresql@novacolor.ca>
@ 2004-02-20 14:24 ` Achilleus Mantzios <achill@matrix.gatewaynet.com>
1 sibling, 0 replies; 5+ messages in thread
From: Achilleus Mantzios @ 2004-02-20 14:24 UTC (permalink / raw)
To: mark <postgresql@novacolor.ca>; +Cc: pgsql-sql
O kyrios mark egrapse stis Feb 20, 2004 :
> Hello,
>
> Is it possible to create a database function that mimics the C function atof?
> I'm guessing it should look something like this:
>
> create function atof(varchar) returns float
> as '??????'
> language ????
> returns null on null input;
Simply use
# select '<some float>'::text::float4;
or
create function atof(varchar) returns float language sql as 'select
$1::text::float' returns null on null input;
>
> Thanks,
>
> Mark
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>
--
-Achilleus
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: create function atof?
2004-02-20 14:04 create function atof? mark <postgresql@novacolor.ca>
@ 2004-02-20 14:54 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 5+ messages in thread
From: Tom Lane @ 2004-02-20 14:54 UTC (permalink / raw)
To: postgresql@novacolor.ca; +Cc: pgsql-sql
mark <postgresql@novacolor.ca> writes:
> Is it possible to create a database function that mimics the C function atof?
Just cast.
There doesn't seem to be a pg_cast entry for varchar to float8, but you
could cast to text and then float8, or you could use functional notation
for the cast (which is a tad more forgiving than CAST or ::).
regression=# select '1234.5'::varchar::float8;
ERROR: cannot cast type character varying to double precision
regression=# select '1234.5'::text::float8;
float8
--------
1234.5
(1 row)
regression=# select float8('1234.5'::varchar);
float8
--------
1234.5
(1 row)
Or write a plpgsql function that simply tries to return its input.
I believe that whenever plpgsql is called on to make a type conversion,
it will invoke the output function of the given type and then the input
function of the other type, so it will work for any cases where the
external textual representation looks the same.
regression=# create function atof(varchar) returns float as
regression-# 'begin
regression'# return $1;
regression'# end' language plpgsql strict immutable;
CREATE FUNCTION
regression=# select atof('1234.5');
atof
--------
1234.5
(1 row)
regression=# select atof('zit');
ERROR: invalid input syntax for type double precision: "zit"
CONTEXT: PL/pgSQL function "atof" while casting return value to function's return type
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2004-06-06 02:16 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-02-20 14:01 create function atof? Mark Tabash <mark.tabash@novacolor.ca>
2004-06-06 02:16 ` Yasir Malik <ymalik@cs.stevens-tech.edu>
2004-02-20 14:04 create function atof? mark <postgresql@novacolor.ca>
2004-02-20 14:24 ` Achilleus Mantzios <achill@matrix.gatewaynet.com>
2004-02-20 14:54 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox