我试图将date_trunc函数用于从列中选择参数的特定时区。
没有'在时区':
SELECT (date_trunc('day', transactions.created_at))::DATE AS period_start FROM transactions LIMIT 1
当传入的参数是字符串时,它也有效:
SELECT (date_trunc('day', TIMESTAMP '2015-09-04 16:00:00' at time zone '+08:00'))::DATE AS period_start FROM transactions LIMIT 1
但是当我试图将两者结合起来时:
SELECT (date_trunc('day', TIMESTAMP transactions.created_at at time zone '+08:00'))::DATE AS period_start FROM transactions LIMIT 1
它在"交易"'附近或附近给了我一个语法错误。
我尝试使用to_char将其转换为字符串,但也没有运气。我怎样才能做到这一点?
答案 0 :(得分:0)
如果您想要投射created_at field
,则必须按如下方式编写:
CAST (transactions.created_at as timestamp)
所以你的最终查询应该是这样的:
SELECT (date_trunc('day', CAST (transactions.created_at as timestamp) at time zone '+08:00'))::DATE AS period_start
FROM transactions LIMIT 1