给定三个单独的日期字段:
完成_date:4/27/2017Due_date:2016年7月22日最新_DATE:7/20/2018
使用notatediff(mm,due_date,最新_date(,我可以将结果计算为24。
我需要能够(在几个月内(弄清楚完成日期在该范围内。结果应为21。
我认为您误解了结果,或者在提供日期时会出现错字。如果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 |
+-----------------+----------+-------------+-----------------------+-------------------------+--------------------------------+