给出三个单独的日期字段:
COMPLETION_DATE:4/27/2017 DUE_DATE:2016年7月22日 截止日期:7/20/2018
使用DATEDIFF(MM,DUE_DATE,LATEST_DATE),我可以将结果计算为24。
然后我需要能够弄清楚(在几个月内)完成日期在该范围内。结果应该是21。
答案 0 :(得分:0)
我认为您错误地估算了结果,或者在提供日期时输错了。如果completion_date
为2018-04-27
而不是您问题中的2017-04-27
,则结果为21
。
create table t (completion_date date, due_date date, latest_date date);
insert into t values ('20180427','20160722','20180720');
select
completion_date = convert(char(7),completion_date)
, due_date = convert(char(7),due_date)
, latest_date = convert(char(7),latest_date)
, months_for_completion = datediff(month,due_date,latest_date)
, months_until_completion = datediff(month,due_date,completion_date)
, months_remaining_at_completion = datediff(month,completion_date,latest_date)
from t
rextester演示:http://rextester.com/DEV75985
返回:
+-----------------+----------+-------------+-----------------------+-------------------------+--------------------------------+
| completion_date | due_date | latest_date | months_for_completion | months_until_completion | months_remaining_at_completion |
+-----------------+----------+-------------+-----------------------+-------------------------+--------------------------------+
| 2018-04 | 2016-07 | 2018-07 | 24 | 21 | 3 |
+-----------------+----------+-------------+-----------------------+-------------------------+--------------------------------+