-2

I have basic query:

SELECT A.Username
    ,EH.[From]
    ,EH.[End]
    ,DATEDIFF(d, [From], [End]) AS HolidaysInDays
FROM EmployeHoliday EH
LEFT JOIN Admin A ON EH.UserId = A.AdminId
WHERE EH.IsActive = 1

If employee apply leave(LeaveStart = "17 Dec 2019" to LeaveEnd = "19 Dec 2019"), when I execute my query the output is HolidaysInDays = 2 but the actual Days is 3. So, how can I resolve this problem?

My output is as shown in the below image.

enter image description here

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
Vikas
  • 71
  • 10

2 Answers2

1

When using DATEDIFF, the end date is an exclusive date so is not factored into the calculation:

PRINT DATEDIFF(d, '2019-12-17', '2019-12-19')

Output:

2

To add the final date in, just add 1 to the result:

PRINT DATEDIFF(d, '2019-12-17', '2019-12-17') + 1
PRINT DATEDIFF(d, '2019-12-17', '2019-12-18') + 1
PRINT DATEDIFF(d, '2019-12-17', '2019-12-19') + 1

Output:

1
2
3
Martin
  • 16,093
  • 1
  • 29
  • 48
0

Think of it like this: what is 7 - 5?

Boom.