views:

73

answers:

2

I would like to convert a given date and integer to a timestamp in a pl/pgsql function. i've never done anything with pl/pgsql before, so i'm somewhat at a loss.

Thanks to the answer by Pablo Santa Cruz, it got this far:

CREATE OR REPLACE FUNCTION to_my_timestamp(mydate date, timeint integer) RETURNS timestamp AS $$
DECLARE
    myhours   integer := timeint / 10000;
    myminutes integer := timeint % 10000 / 100;
    myseconds integer := timeint % 100;
    timestring  text    := myhours || ':' || myminutes || ':' || myseconds;
BEGIN
    RETURN (mydate::text || ' ' || timestring)::timestamp;
$$ LANGUAGE plpgsql;

select to_my_timestamp('2010-08-12',123456);

However, this doesn't seem to work for me yet. This is the error I get using pgadmin3 on Ubuntu:

ERROR:  syntax error at end of input
LINE 9: $$ LANGUAGE plpgsql;
        ^

********** Error **********

ERROR: syntax error at end of input
SQL state: 42601
Character: 377

Any help is greatly appreciated :)

A: 

Try changing your return value to:

RETURN (mydate::text || ' ' || timestring)::timestamp;
Pablo Santa Cruz
thanks for your answer! but unfortunately, this doesn't work:ERROR: syntax error at or near ":"
andreash
That should be mydate::text - two colons
Magnus Hagander
@Magnus, @andrash: Yah! Sorry 'bout that type. Just fixed the answer. Try it. It should work.
Pablo Santa Cruz
hey Pablo, thanks. However, still no luck :( see my edited question above for error message.
andreash
A: 

You're missing END;:

CREATE OR REPLACE FUNCTION to_my_timestamp(mydate date, timeint integer) RETURNS timestamp AS $$
DECLARE
    myhours   integer := timeint / 10000;
    myminutes integer := timeint % 10000 / 100;
    myseconds integer := timeint % 100;
    timestring  text    := myhours || ':' || myminutes || ':' || myseconds;
BEGIN
    RETURN (mydate::text || ' ' || timestring)::timestamp;
END;
$$ LANGUAGE plpgsql;
Stephen Denne
jeez, stupid me ... thanks!
andreash