日期型数据不必更多描述,直奔主题吧
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
https://www.cnblogs.com/mchina/archive/2013/04/15/3010418.html