Search code examples
sql-serverdatediff

DATEDIFF in SQL Server


I am using DATEDIFF, however, I got unexpected results.

I tried to calculate the Reminder column by minus 5 days from the calculated deadline column.

I received some results but they don't make sense.

Below is my query:

SELECT n.LastReceiptDate, 
DATEADD(dd, 10, n.LastReceiptDate) AS deadline, 
DATEDIFF(dd, 5, DATEADD(dd, 10, n.LastReceiptDate)) AS Reminder 
FROM Table n;

enter image description here


Solution

  • SELECT n.LastReceiptDate, 
    DATEADD(dd, 10, n.LastReceiptDate) AS deadline, 
    DATEADD(dd, -5, DATEADD(dd, 10, n.LastReceiptDate)) AS Reminder 
    FROM Table n;