Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在oracle中计算年、月、日_Sql_Oracle_Date - Fatal编程技术网

Sql 在oracle中计算年、月、日

Sql 在oracle中计算年、月、日,sql,oracle,date,Sql,Oracle,Date,我正在开发一个程序,该程序采用开始日期和结束日期,并返回年、月和日的不同值。每个类别都有自己的代码部分,这是我一直在使用的代码,到目前为止,它一直是半精确的,直到几天(有时是几个月。我甚至不想在这一点上愚弄闰年) Ex Start:04/10/2000 End:04/10/2006应该给我6年零个月零天 年份代码: SELECT trunc(months_between((to_date(:main_DT_DateEnd1,'MM/DD/YYYY')),(to_date(:main_DT_Da

我正在开发一个程序,该程序采用开始日期和结束日期,并返回年、月和日的不同值。每个类别都有自己的代码部分,这是我一直在使用的代码,到目前为止,它一直是半精确的,直到几天(有时是几个月。我甚至不想在这一点上愚弄闰年)

Ex Start:04/10/2000 End:04/10/2006应该给我6年零个月零天

年份代码:

SELECT
 trunc(months_between((to_date(:main_DT_DateEnd1,'MM/DD/YYYY')),(to_date(:main_DT_DateBeg1,'MM/DD/YYYY'))) / 12) as "Years1"
FROM dual
月份代码:

SELECT
  trunc(mod(months_between((to_date(:main_DT_DateEnd1,'MM/DD/YYYY')),(to_date(:main_DT_DateBeg1,'MM/DD/YYYY'))), 12)) as "Months1"
FROM dual
天代码:我尝试了多个版本,但都没有太大成功。例如,我可以计算天与天之间的总天数,但由于某些天中有不同的月份,所以分割变得更麻烦。这是我得到的最接近的一个,如果天数相同,则不需要计算,否则使用子字符串减去它们。 (一)

选择
案例
当substr((截止日期(:main_dtu DateBeg1,'MM/DD/YYYY'))时,4,5)=substr((截止日期(:main_dtu DateEnd1,'MM/DD/YYYY')),4,5)
然后0
当substr((截止日期(:main_dtu DateBeg1,'MM/DD/YYYY'))4,5)

感谢您的时间,对于那些想知道这是一个工作经验计算器的人:)

请参考这里的示例:

您完全可以使用以下逻辑来计算年份和月份。就天数而言,2月28日+1个月收益率(3月31日)有点棘手

select extract(year from (d2 - d1) year to month), 
       extract(month from (d2 - d1) year to month),
       add_months(d1, extract(year from (d2 - d1) year to month)*12 + extract(month from (d2 - d1) year to month)) - d2 
from (
select date'2014-08-27' as d2, date'2002-02-27' as d1
  from dual
)  

我问了同样的问题,得到了很好的回答。这有帮助吗?我会调查这个斯坦利,我有一个类似的答案,使用不同的月份格式(也许这就是区别),谢谢lalit,我也会调查这个。我尝试了6个帖子的回复,结果真的失败了。谢谢你迄今为止的帮助@斯坦利,我从你的帖子里得到了我想要的答案,谢谢
select extract(year from (d2 - d1) year to month), 
       extract(month from (d2 - d1) year to month),
       add_months(d1, extract(year from (d2 - d1) year to month)*12 + extract(month from (d2 - d1) year to month)) - d2 
from (
select date'2014-08-27' as d2, date'2002-02-27' as d1
  from dual
)