Ajendra Prasad
Ajendra Prasad

Reputation: 327

How to find number of days from given days?

I have two dates and want to find number of days including both dates, I tried something like this:

select datediff("d", '10/11/2012', '10/12/2012')

It is showing 1, but I want the result as 2 because we have 2 days 11 and 12.

Upvotes: 0

Views: 140

Answers (3)

t-clausen.dk
t-clausen.dk

Reputation: 44336

In case you are also operating with negative differences, adding 1 may not give you the answer you are looking for, you can handle those situations like this:

declare @from datetime = '2012-10-11'
declare @to   datetime = '2012-10-10'

select datediff(day, @from, @to) 
 + case when datediff(day, @from, @to) < 0 then -1 else 1 end

Upvotes: 1

Jaimal Chohan
Jaimal Chohan

Reputation: 8645

Well datediff will always find the difference between 2 days, as you've noted, for your example, this is 1. if you wanted to make this inclusive, then why not just +1 to the result?

select datediff("d", '10/11/2012', '10/12/2012') + 1

Upvotes: 1

Andomar
Andomar

Reputation: 238296

The latest version of SQL introduced support for adding integers! Yay.

select datediff("d", '10/11/2012', '10/12/2012') + 1
                                                ^^^^^

Upvotes: 9

Related Questions