Question

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 :)

Was it helpful?

Solution

Try changing your return value to:

RETURN (mydate::text || ' ' || timestring)::timestamp;

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top