2017-03-16 31 views
2

需要將1/1000秒分辨率的時間戳轉換爲1/100分辨率。我可以使用to_char(timestamp, text)格式化功能來實現此目的,但需要在此處使用text的幫助。時間戳格式 - 從1/1000秒到1/100秒

輸入表(注 - 這裏的時間戳存儲爲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 | 
+------------------------+ 
+0

只是使用'timestamp(2)' –

回答

6

您可以在括號中註明它,就像這裏:

t=# select now()::timestamp(2); 
      now 
------------------------ 
2017-03-16 09:55:21.15 
(1 row) 

a S OP注意到http://rextester.com/CBZ17212會產生不同的結果,那麼在psql運行:

t=# CREATE TABLE Table1 
t-#  ("ms1000_val" varchar(23)) 
t-# ; 
CREATE TABLE 
t=# 
t=# INSERT INTO Table1 
t-#  ("ms1000_val") 
t-# VALUES 
t-#  ('2017/02/20 08:27:17.892'), 
t-#  ('2017/02/20 08:23:43.891'), 
t-#  ('2017/02/20 08:24:41.897'), 
t-#  ('2017/02/20 08:28:09.893') 
t-# ; 
INSERT 0 4 
t=# select ms1000_val::timestamp(2) as time_formatted 
t-#  from Table1; 
    time_formatted 
------------------------ 
2017-02-20 08:27:17.89 
2017-02-20 08:23:43.89 
2017-02-20 08:24:41.9 
2017-02-20 08:28:09.89 
(4 rows) 
+0

檢查此代碼牆 - 它不會給出與上面的代碼相同的結果 - 完全刪除ms部分 - http://rextester.com/CBZ17212 – user3206440

+0

您說「要轉換1/1000秒的時間戳「not varchar ... –

+0

很高興被糾正。更新了問題。 – user3206440

1

證明這一點,我使用TO_CHAR和SUBSTR和我得到的格式爲yyyy/MM/DD ......

select substr(to_char(now(),'yyyy/MM/dd HH:mm:ss MS'),0,length('yyyy/MM/dd HH:mm:ss MS')+1);