日期操作--日期之差


- 日期之差
  --获取秒差
SELECT round(date_part('epoch', TIMESTAMP '2019-05-05 12:11:20' - TIMESTAMP '2019-05-05 10:10:10'));
  --获取分钟差
SELECT round(date_part('epoch', TIMESTAMP '2019-05-05 12:11:20' - TIMESTAMP '2019-05-05 10:10:10')/60);
  --获取小时差
SELECT round(date_part('epoch', TIMESTAMP '2019-05-05 12:11:20' - TIMESTAMP '2019-05-05 10:10:10')/60/60);
  --获取天数差
SELECT Date('2019-06-05') - Date('2019-05-03');
--获取月份差
select extract(year from age(TIMESTAMP '2018-04-05',TIMESTAMP '2017-02-04')) * 12  + extract(MONTH from age(TIMESTAMP '2019-04-05',TIMESTAMP '2017-02-04'));
--获取年份差
SELECT extract(year from age(TIMESTAMP '2018-04-05',TIMESTAMP '2017-02-04'));
- 

---2020-8-27日增:
--获取当前年月
SELECT TO_CHAR(now(), 'yyyy-mm')

posted on 2020-07-30 09:18  一贯可乐  阅读(2937)  评论(0编辑  收藏  举报



123