2017-03-16 88 views
1

需要將1/1000秒分辨率的時間戳轉換爲1/100分辨率。我可以使用to_char(timestamp, text)格式化功能來達到此目的,但是需要使用text的幫助。 Postgres的做法是hereNetezza時間格式化

輸入表(注 - 這裏的時間戳存儲爲varchar)

+-------------------------+ 
|  ms1000_val  | 
+-------------------------+ 
| 2017/02/20 08:27:17.899 | 
| 2017/02/20 08:23:43.894 | 
| 2017/02/20 08:24:41.894 | 
| 2017/02/20 08:28:09.899 | 
+-------------------------+ 

輸出表

+------------------------+ 
|  ms100_val  | 
+------------------------+ 
| 2017/02/20 08:27:17.89 | 
| 2017/02/20 08:23:43.89 | 
| 2017/02/20 08:24:41.89 | 
| 2017/02/20 08:28:09.89 | 
+------------------------+ 

回答

1

試試這個

select cast(to_char(sub.field,'YYYY-MM-DD HH24:MI:SS') as timestamp) 
+ interval '10 millisecond' * (cast(to_char(sub.field,'MS') as integer)/10) as converted_value 

from (
    select to_timestamp('2017/02/20 08:27:17.899','YYYY/MM/DD HH24:MI:SS.MS') as field 
    union 
    select to_timestamp('2017/02/20 08:23:43.894','YYYY/MM/DD HH24:MI:SS.MS') 
    union 
    select to_timestamp('2017/02/20 08:24:41.894','YYYY/MM/DD HH24:MI:SS.MS') 
    union 
    select to_timestamp('2017/02/20 08:28:09.899','YYYY/MM/DD HH24:MI:SS.MS') 
    ) sub