首页 > 其他 > 详细

Postgres-日期型数据

时间:2020-01-02 14:37:00      阅读:75      评论:0      收藏:0      [点我收藏+]

日期型数据不必更多描述,直奔主题吧

 

cast:字符转换成日期

select cast(20190101 as date);
-- 输出 "2019-01-01"

select 20190101::date;
-- 输出 "2019-01-01"

 

age:日期相减

 两个参数:两日期相减

SELECT AGE(timestamp 2017-01-26, timestamp 2018-01-30);
-- 输出 "-1 years -4 days"

一个参数:以 current_date(午夜) 为标准

SELECT AGE(timestamp 2017-01-26);
-- 现在时刻 2020-01-02
-- 输出 "2 years 11 mons 6 days"

 

获取时间

有很多,具体见参考资料

select current_date;    -- 当前日期
select current_time;    -- 当前时间,带时区
select localtime;

 

获取年月日

select extract(year from 2019-01-01::date);    -- 2019

year 可换成 month、day

 

 

 

未完待续...

 

 

参考资料:

https://www.yiibai.com/postgresql/postgresql-date-time.html

Postgres-日期型数据

原文:https://www.cnblogs.com/yanshw/p/12132226.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!