如何在pl / pgsql函数中将int转换为时间

时间:2010-08-30 12:32:23

标签: postgresql plpgsql

我想将给定的日期和整数转换为pl / pgsql函数中的时间戳。我以前从未对pl / pgsql做过任何事情,所以我有些不知所措。

感谢Pablo Santa Cruz的回答,它达到了这个目的:

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

然而,这对我来说似乎并不适用。这是我在Ubuntu上使用pgadmin3时出现的错误:

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

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

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

非常感谢任何帮助:)

2 个答案:

答案 0 :(得分:1)

尝试将返回值更改为:

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

答案 1 :(得分:1)

你错过了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;