数据库之postgreSql时间计算,例如获取前一天、后一天等。

一、获取系统时间函数

1.1 获取当前完整时间

select now();
select now(); now ------------------------------- 2013-04-12 15:39:40.399711+08 (1 row)
current_timestamp 同 now() 函数等效。
 select current_timestamp; now ------------------2013-04-12 15:40:22.398709+08 (1 row) 

1.2 获取当前日期

select current_date;
select current_date; date ------------ 2013-04-12 (1 row)

1.3 获取当前时间


select current_time;
select current_time; timetz -------------------- 15:43:31.101726+08 (1 row)

二、时间的计算

select now(); now ------------------------------- 2013-04-12 15:47:13.244721+08 (1 row)

2.1 两年后


 select now() + interval '2 years';  -- 2015-04-12 15:49:03.168851+08 (1 row) 
 
select now() + interval '2 year'; -- 2015-04-12 15:49:12.378727+08 (1 row) 
 
select now() + interval '2 y'; -- 2015-04-12 15:49:25.46986+08 (1 row)
 
select now() + interval '2 Y'; -- 2015-04-12 15:49:28.410853+08 (1 row) 
 
select now() + interval '2Y'; -- 2015-04-12 15:49:31.122831+08 (1 row)

2.2 一个月后

select now() + interval '1 month'; -- 2013-05-12 15:51:22.24373+08 (1 row) 
 
select now() + interval 'one month'; 
ERROR: invalid input syntax for type interval: "one month" LINE 1: select now() + interval 'one month';

注意:这里要区别开'1 month'和'one month',语法值能满足第一种写法,即写入阿拉伯数字。

2.3 三周前

select now() - interval '3 week';

2.4 十分钟后

select now() + '10 min'; 
说明:
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)
select age(timestamp '2007-09-15'); age ------------------------ 5 years 6 mons 27 days (1 row)

三、时间字段的截取

在开发过程中,经常要取日期的年,月,日,小时等值,PostgreSQL 提供一个非常便利的EXTRACT函数。

EXTRACT(field FROM source)

field 表示取的时间对象,source 表示取的日期来源,类型为 timestamp、time 或 interval。

3.1 取年份

select extract(year from now()); date_part ----------- 2013 (1 row)

3.2 取月份

select extract(month from now()); date_part ----------- 4 (1 row)

select extract(day from timestamp '2013-04-13'); date_part ----------- 13 (1 row) 

SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute'); date_part ----------- 40 (1 row)

3.3 查看今天是一年中的第几天

select extract(doy from now()); date_part ----------- 102 (1 row)

3.4 查看现在距1970-01-01 00:00:00 UTC 的秒数

select extract(epoch from now()); date_part ------------------ 1365755907.94474 (1 row)

3.5 把epoch 值转换回时间戳

SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1369755555 * INTERVAL '1 second'; ------------------------ 2013-05-28 23:39:15+08 (1 row) 

因为要做一个where条件 
adate varchar(12) 201610101010 

要筛选 adate > 系统时间 的数据。

你的adate的格式是否为年月日时分(示例就变成2016-10-10 10:10),可以用to_timestamp转换为日期时间值:
to_timestamp(adate, 'YYYYMMDDHH24MI')
不过,从你的需求来看,adate可能为数据表的字段,要提高执行效率的化,应该是将系统时间转换为字串来进行比较,而不是将字段值转换为日期时间值来比较。因为将字段转换为日期时间值后比较的化,肯定不能使用索引了(除非你定义了转换后的索引),而且每次查询都有左转换效率太差了。故这样来做筛选条件会比较好些:
where adate > to_char(current_timestamp, 'YYYYMMDDHH24MI')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值