PostgreSQL:两个日期之间的日/月/年 [英] PostgreSQL: days/months/years between two dates

查看:425
本文介绍了PostgreSQL:两个日期之间的日/月/年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在PostgreSQL中实现SQLServer函数datediff的方法。也就是说,

I am looking for a way to implement the SQLServer-function datediff in PostgreSQL. That is,

datediff(dd, '2010-04-01', '2012-03-05') = 704 // 704 changes of day in this interval
datediff(mm, '2010-04-01', '2012-03-05') = 23  // 23 changes of month
datediff(yy, '2010-04-01', '2012-03-05') = 2   // 2 changes of year

我知道我可以通过简单的使用减法来做'dd',但是有其他两个想法?

I know I could do 'dd' by simply using substraction, but any idea about the other two?

推荐答案

select age('2010-04-01', '2012-03-05'),
       date_part('year',age('2010-04-01', '2012-03-05')),
       date_part('month',age('2010-04-01', '2012-03-05')),
       date_part('day',age('2010-04-01', '2012-03-05'));

此功能将使您完整年,月,日...两个日期之间。对于日期更改的数量 - 我们链接由revoua提供: http://www.sqlines.com/postgresql / how-to / datediff

This functions will give you full years, month, days ... between two dates. For the number of date changes - us the link, provided by revoua : http://www.sqlines.com/postgresql/how-to/datediff

这篇关于PostgreSQL:两个日期之间的日/月/年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆