本文章來給各位朋友介紹關於PostgreSQL的時間日期函數用法,有需要了解PostgreSQL的時間與日期在sql中的使用方法可參考參考本教程 。
一、獲取系統時間函數
1.1 獲取當前完整時間
代碼如下 復制代碼select now();
david=# select now();
now
-------------------------------
2013-04-12 15:39:40.399711+08
(1 row)
david=# current_timestamp 同 now() 函數等效。
david=# select current_timestamp;
now
-------------------------------
2013-04-12 15:40:22.398709+08
(1 row)
david=# 1.2
獲取當前日期
select current_date;
david=# select current_date;
date
------------
2013-04-12
(1 row)
david=# 1.3
獲取當前時間
select current_time;
david=# select current_time;
timetz
--------------------
15:43:31.101726+08
(1 row)
david=#二、
時間的計算
代碼如下 復制代碼
david=# select now();
now
-------------------------------
2013-04-12 15:47:13.244721+08
(1 row)
david=#2.1
兩年後
代碼如下 復制代碼
david=# select now() + interval '2 years';
?column?
-------------------------------
2015-04-12 15:49:03.168851+08
(1 row)
david=# select now() + interval '2 year';
?column?
-------------------------------
2015-04-12 15:49:12.378727+08
(1 row)
david=# select now() + interval '2 y';
?column?
------------------------------
2015-04-12 15:49:25.46986+08
(1 row)
david=# select now() + interval '2 Y';
?column?
-------------------------------
2015-04-12 15:49:28.410853+08
(1 row)
david=# select now() + interval '2Y';
?column?
-------------------------------
2015-04-12 15:49:31.122831+08
(1 row)
david=# 2.2
一個月後
代碼如下 復制代碼
david=# select now() + interval '1 month';
?column?
------------------------------
2013-05-12 15:51:22.24373+08
(1 row)
david=# select now() + interval 'one month';
ERROR: invalid input syntax for type interval: "one month"
LINE 1: select now() + interval 'one month';
^
david=# 2.3
三周前
代碼如下 復制代碼
david=# select now() - interval '3 week';
?column?
-------------------------------
2013-03-22 16:00:04.203735+08
(1 row)
david=# 2.4 十分鐘後
david=# select now() + '10 min';
?column?
-------------------------------
2013-04-12 16:12:47.445744+08
(1 row)
david=#
說明:
interval 可以不寫,其值可以是:
Abbreviation Meaning
Y Years
M Months (in the date part)
W Weeks
D Days
H Hours
M Minutes (in the time part)
S Seconds
2.5 計算兩個時間差
使用 age(timestamp, timestamp)
代碼如下 復制代碼
david=# select age(now(), timestamp '1989-02-05');
age
----------------------------------------
24 years 2 mons 7 days 17:05:49.119848
(1 row)
david=#
david=# select age(timestamp '2007-09-15');
age
------------------------
5 years 6 mons 27 days
(1 row)
david=#
三、時間字段的截取
在開發過程中,經常要取日期的年,月,日,小時等值,PostgreSQL 提供一個非常便利的EXTRACT函數。
EXTRACT(field FROM source)field 表示取的時間對象,source 表示取的日期來源,類型為 timestamp、time 或 interval。
3.1 取年份
代碼如下 復制代碼
david=# select extract(year from now());
date_part
-----------
2013
(1 row)
david=# 3.2 取月份
david=# select extract(month from now());
date_part
-----------
4
(1 row)
david=#
david=# select extract(day from timestamp '2013-04-13');
date_part
-----------
13
(1 row)
david=#
david=# SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
date_part
-----------
40
(1 row)
david=# 3.3 查看今天是一年中的第幾天
david=# select extract(doy from now());
date_part
-----------
102
(1 row)
david=# 3.4
查看現在距1970-01-01 00:00:00 UTC 的秒數
david=# select extract(epoch from now());
date_part
------------------
1365755907.94474
(1 row)
david=# 3.5 把epoch 值轉換回時間戳
david=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1369755555 * INTERVAL '1 second';
?column?
------------------------
2013-05-28 23:39:15+08
(1 row)
david=#
--取day
skytf=> select extract(day from now());
date_part
-----------
3
(1 row)
skytf=> select extract(day from timestamp '2011-06-03');
date_part
-----------
3
(1 row)
skytf=> select timestamp '2011-06-03';
timestamp
---------------------
2011-06-03 00:00:00
(1 row)
--取小時
skytf=> select extract (hour from now());
date_part
-----------
14
(1 row)
--取分鐘
skytf=> select extract (minute from now());
date_part
-----------
59
(1 row)
--取秒
skytf=> select extract (second from now());
date_part
-----------
46.039333
(1 row)
--取所在哪個星期
skytf=> select extract (week from now());
date_part
-----------
22
(1 row)
以上是基本的PG時間/日期函數使用,可滿足一般的開發運維應用