如何使用 DATEDIFF 计算剩余天数

How to calculate remaining days using DATEDIFF

我写了一个基本查询来显示所有项目、截止日期和完成状态,但我想扩展我的查询以显示完成天数,它应该显示每个未完成项目的到期剩余天数。我如何使用 DATEDIFF 功能来达到预期的效果?

我可以使用

=IIF(DATEDIFF(duedate.Day, Fields!Created.Value, Today) < 30, 1, 0)

输出:

id  name    duedate     today   
---------------------------------------------------------------
1   Alpha   2040-01-01  2018-07-01T01:49:43.943Z    Incomplete
2   Bravo   2030-03-01  2018-07-01T01:49:43.943Z    Incomplete
3   Charlie 2017-02-01  2018-07-01T01:49:43.943Z    Complete
4   Delta   2017-04-01  2018-07-01T01:49:43.943Z    Complete

我当前的查询:

SELECT 
    id, name, duedate, GETDATE() AS today,
    CASE 
       WHEN duedate <= CURRENT_TIMESTAMP THEN 'Complete'
       WHEN duedate >= CURRENT_TIMESTAMP THEN 'Incomplete'
    END
FROM 
    Project
ORDER BY 
    Project.name ASC;

想要的结果:

id  name    duedate     today                       Status      daystocomp
--------------------------------------------------------------------------
1   Alpha   2040-01-01  2018-07-01T01:49:43.943Z    Incomplete  8030 or 22y1m1d
2   Bravo   2030-03-01  2018-07-01T01:49:43.943Z    Incomplete  4380 or 12y1m1d
3   Charlie 2017-02-01  2018-07-01T01:49:43.943Z    Complete    0
4   Delta   2017-04-01  2018-07-01T01:49:43.943Z    Complete    0

是的,您可以使用 datediff():

SELECT id, name, duedate, GETDATE() AS today,
       (CASE WHEN duedate <= CURRENT_TIMESTAMP THEN 'Complete'
             WHEN duedate > CURRENT_TIMESTAMP THEN 'Incomplete'
        END),
       (CASE WHEN duedate > CURRENT_TIMESTAMP
             THEN DATEDIFF(day, CURRENT_TIMESTAMP, duedate)
        END) as daystocomp
FROM Project
ORDER BY Project.name asc;