how to convert int to time in a pl/pgsql function
I would like to convert a given date and integer to a timestamp in a pl/pgsql function. i've never done anyt开发者_如何学运维hing 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 :)
Try changing your return value to:
RETURN (mydate::text || ' ' || timestring)::timestamp;
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;
精彩评论