两个日期之间的月份功能 [英] Months between two dates function
本文介绍了两个日期之间的月份功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在postgres中,我使用的是提取函数。
eg.like
选择
提取(年龄起(current_date,'2012-12-09 '))* 12
+
提取(从年龄起的月份(current_date,'2012-12-09'))
在postgres中是否有其他的方法(内置函数)??
解决方案
很容易在PostgreSQL中重新实现,只需使用SQL函数整理你已经得到的内容即可:
create function months_of(间隔)
返回int严格不可变语言sql as $$
选择提取(从$ 1开始的年份):: int * 12 +提取(月份从$ 1):: int
$$;
创建函数months_between(date,date)
返回int严格不可变语言sql as $$
select abs(months_of(age($ 1,$ 2)))
$$;
现在 select months_between('1978-06-20','2011 -12-09')
产生401。
In oracle i can find out no:of months between using MONTHS_BETWEEN function.
In postgres i am using extract function for this. eg.like
select
extract(year from age(current_date, '2012-12-09')) * 12
+
extract(month from age(current_date, '2012-12-09'))
Is there any other ways(built in functions) in postgres??
解决方案
This is easy to re-implement in PostgreSQL just using SQL functions to tidy up what you've already got:
create function months_of(interval)
returns int strict immutable language sql as $$
select extract(years from $1)::int * 12 + extract(month from $1)::int
$$;
create function months_between(date, date)
returns int strict immutable language sql as $$
select abs(months_of(age($1, $2)))
$$;
And now select months_between('1978-06-20', '2011-12-09')
produces 401.
这篇关于两个日期之间的月份功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文