PostgreSQL日期和时间函数

PostgreSQL的日期和时间函数跟其他数据库还是有很大区别的,在工作中,一方面是经验的积累,经验的积累主要靠熟练掌握其数据库中的各种语法,函数,特性等,方便在需要用到的时候,拿来即用。

AGE()函数

主要有age(timestamp,timestamp)计算两个时间间隔的年龄
计算下我的年龄(不小心暴露了我的年龄…)

postgres=# select age(timestamp '2019-03-28',timestamp '1990-11-14');
           age
-------------------------
 28 years 4 mons 14 days
(1 行记录)

age(timestamp)一个参数的,是计算当前系统时间的年龄。及用当前系统时间减去timestamp计算得到的年龄。

postgres=# select age(timestamp '1990-11-14');
           age
-------------------------
 28 years 4 mons 14 days
(1 行记录)

当前DATE/TIME()

current_date:提供当前日期

postgres=# select current_date;
 current_date
--------------
 2019-03-28
(1 行记录)

current_time:提供带时区的值

postgres=# select current_time;
    current_time
--------------------
 09:46:56.642668+08
(1 行记录)

current_timestamp:提供带时区的值

postgres=# select current_timestamp;
       current_timestamp
-------------------------------
 2019-03-28 09:47:39.950145+08
(1 行记录)

current_time(precision):可以选择使用precision参数,这将使结果在四分之一秒的范围内四舍五入到数位数。在其他类型的数据库我暂时还没有发现此功能。
同样也提供current_timestamp(precision)

postgres=# select current_time(2);
  current_time
----------------
 09:52:24.12+08
(1 行记录)

localtime:提供没有时区的值。

postgres=# select localtime;
    localtime
-----------------
 09:50:06.073771
(1 行记录)

localtimestamp:提供没有时区的值

postgres=# select localtimestamp;
       localtimestamp
----------------------------
 2019-03-28 09:50:52.235411
(1 行记录)

同样提供带精度的localtime(precision)和localtimestamp(precision)

猜你喜欢

转载自blog.csdn.net/huangbaokang/article/details/88862703