2015-09-04 54 views
0

我想使用date_trunc函數與從列中選擇參數的特定時區。Postgres'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

如果你想投你' created_at'字段,你必須這樣寫:'cast(transactions.created_at as timestamp)' – Houari

+0

@Houari我覺得這很愚蠢,那很簡單。非常感謝你的幫助!你想寫這個答案? – mountriv99

回答

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