Click here to Skip to main content
15,891,136 members

Comments by GregStevens (Top 13 by date)

GregStevens 14-Jul-11 15:20pm View    
Deleted
Fantastic! I will update the tip with this version. Im' only going to modify it slightly because you need to substract 2, not 1, if DATEPART(dw, @EndDate) < DatePart(dw, @StartDate)
GregStevens 14-Jul-11 10:08am View    
Deleted
Well, as I suspected: it returns 4, even though the correct answer should be 2. (The number of week days BETWEEN 7/15 and 7/15 is zero, so you can't count 7/15 itself as 1).

So the way to get this expression to be correct is to add one more term, just like my Oracle example has above: a term that basically says "If the weekday of the EndDate is before the weekdate of the StartDate, then subtract another 2".

If you can give me the syntax for how to do that in SQL server, we can add it to what you've already provided above, and it should be correct!
GregStevens 13-Jul-11 14:55pm View    
Deleted
How fantastically weird. I wonder why?

In the example where StartDate is 7/15/2011 and EndDate is 7/19/2011, the raw difference is 4, and neither the start nor the end is a sunday or saturday, so the last two terms are zero.

As a result, to get a result of 3, DATEDIFF(wk, @StartDate, @EndDate) would have to be.... 1/2 ? That can't be, documentation says that DATEDIFF returns an integer.

Can you verify what DATEDIFF(wk, @StartDate, @EndDate) returns when StartDate is 7/15/2011 and EndDate is 7/19/2011?

GregStevens 13-Jul-11 12:35pm View    
Deleted
P.S. if you have access to an SQL server database, please check, though: it's possible that I am wrong about how (DATEDIFF(wk, @StartDate, @EndDate) functions. If (DATEDIFF(wk, @StartDate, @EndDate) returns 1 when StartDate is 7/15/2011 and EndDate is 7/19/2011, then the above expression will work!
GregStevens 13-Jul-11 12:34pm View    
Deleted
I don't think that works, because it only accommodates 3 of the 4 points in my list above: it starts with the raw difference, it subtracts the full weeks, and it corrects for days that fall on the weekend; however, it does not correct for weekends that occur before a partial week is up.

But it's close! The problem is, I don't think it returns the correct result for 7/15/2011 to 7/19/2011. The answer should be 2. But I think it returns 4.

All it needs it one more expression that basically says "IF the day of the week of StartDate is later than the day-of-the-week End Date, then subtract another 2 days."

Let's figure this out! :-) How would you do that expression in SQL Server?