[英]timestamp formatting - from 1/1000th of a sec to 1/100th of sec
需要將時間戳以1/1000秒分辨率轉換為1/100分辨率。 我可以為此目的使用to_char(timestamp, text)
格式化功能,但是需要幫助text
在這里使用。
輸入表 (注意 - 此處的時間戳存儲為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 |
+------------------------+
您可以在括號中指定它,如下所示:
t=# select now()::timestamp(2);
now
------------------------
2017-03-16 09:55:21.15
(1 row)
正如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)
顯示這個,我使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.