Postgres为时间戳转换创建函数

时间:2013-06-19 06:03:18

标签: function postgresql timestamp plpgsql

我正在尝试创建一个postgres函数,将输入时间戳转换为PDT。这是我的代码:

CREATE OR REPLACE FUNCTION getPdtTimestamp(inTS TIMESTAMP) RETURNS TIMESTAMP AS $$
DECLARE
    outTS TIMESTAMP;
BEGIN
    SELECT TIMESTAMP with time zone inTS AT TIME ZONE 'PDT' INTO outTS;
    RETURN outTS;
END;
$$ LANGUAGE plpgsql;

我得到的错误是:

syntax error at or near "inTS"

我的目标是创建一个如下所示的查询函数:

SELECT TIMESTAMP with time zone '2012-01-01 12:00:00' AT TIME ZONE 'PDT';
// returns a value

提前致谢

1 个答案:

答案 0 :(得分:3)

这应该有效:

CREATE FUNCTION getPdtTimestamp(timestamp) RETURNS TIMESTAMP AS $$ 
    SELECT $1::TIMESTAMP with time zone  AT TIME ZONE 'PDT';
$$ LANGUAGE SQL;