pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
syntax for using function parameter in to_char
2+ messages / 2 participants
[nested] [flat]

* syntax for using function parameter in to_char
@ 2002-02-01 17:57 Kevin Barrett <anon000000@hotmail.com>
  2002-02-01 20:40 ` Re: syntax for using function parameter in to_char Josh Berkus <josh@agliodbs.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Kevin Barrett @ 2002-02-01 17:57 UTC (permalink / raw)
  To: pgsql-sql

I am trying to write a function that will take a big int as a paramter and 
return a formatted date string.  The dates in the system I am using are all 
stored as bigint values ( based Java EPOCH ) to I need this function to 
make some sense of the date.

Here is my code:

CREATE FUNCTION java_date( bigint ) RETURNS text AS '
    select to_char(\'epoch\'::datetime + \'$1 seconds\'::interval, 
\'dd/mm/yyyy hh:mi\') as RESULT
  '
  LANGUAGE 'sql';


The problem seems to be that the $1 in the to_char function is not getting 
parsed with the actual value of $1.

If I execute the following from the command line, I get the expected 
response:

=> select to_char('epoch'::datetime + '1012437127 seconds'::interval, 
'dd/mm/yyyy hh:mi');

     to_char
------------------
 30/01/2002 06:32
(1 row)

All I am trying to do is move the functionality of this SQL statement to a 
function so I can easily reuse it.

Any help would be appreciated.

Kevin Barrett





^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: syntax for using function parameter in to_char
  2002-02-01 17:57 syntax for using function parameter in to_char Kevin Barrett <anon000000@hotmail.com>
@ 2002-02-01 20:40 ` Josh Berkus <josh@agliodbs.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Josh Berkus @ 2002-02-01 20:40 UTC (permalink / raw)
  To: Kevin Barrett <anon000000@hotmail.com>; pgsql-sql

Kevin,

> CREATE FUNCTION java_date( bigint ) RETURNS text AS '
>     select to_char(\'epoch\'::datetime + \'$1 seconds\'::interval, 
> \'dd/mm/yyyy hh:mi\') as RESULT
>   '
>   LANGUAGE 'sql';

Try this instead:

 CREATE FUNCTION java_date( bigint ) RETURNS text AS '  
     select to_char(''epoch''::datetime + 
              (CAST($1 AS VARCHAR) || ''seconds'')::interval, 
               ''dd/mm/yyyy hh:mi'') as RESULT
'
LANGUAGE 'sql';

Got it?  Check, though, a "CAST (bigint as varchar)" may or may not
 work.

-Josh



______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-02-01 20:40 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-02-01 17:57 syntax for using function parameter in to_char Kevin Barrett <anon000000@hotmail.com>
2002-02-01 20:40 ` Josh Berkus <josh@agliodbs.com>

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