简体   繁体   English

Postgres为时间戳转换创建函数

[英]Postgres create function for timestamp conversion

I'm trying to create a postgres function which converts the input timestamp to PDT. 我正在尝试创建一个postgres函数,将输入时间戳转换为PDT。 Here is my code: 这是我的代码:

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;

The error I get here is: 我得到的错误是:

syntax error at or near "inTS"

My goal is to make a function of a query which looks like this: 我的目标是创建一个如下所示的查询函数:

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

Thanks in advance 提前致谢

This should work: 这应该工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM