Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Calculate number of days

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2012-07-16 : 12:39:16
I have a sp where user can pass in parameter like @DaysBack. I use this parameter to calulate how many days this is back from current day like:

declare @DateFrom_yyyy_mm_dd as char(12)
set @DateFrom_yyyy_mm_dd = cast(convert(varchar(8), current_timestamp - @DaysBack, 112) as datetime)


I use this date as starting date for adding up sales quantities by the hour for every day up to current and excluding current day. I am excluding SaturDay and Sunday. I am not worried about holidays at this point.

The @DaysBack parameter can dynamically bet set by user.

Is there a way to find out how many days are included in the selection excluding any Saturday or Sunday Thank you.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-16 : 15:24:57
See this thread: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=176036
In your case it would be something like this:
DECLARE @DaysBack INT = 20, @dateFrom DATETIME;
SET @dateFrom = DATEADD(dd,-@DaysBack,GETDATE());
SELECT @dateFrom, DATEDIFF(DAY, @dateFrom, GETDATE()) - DATEDIFF( wk, @dateFrom, GETDATE())*2 AS WeekDays;
Go to Top of Page
   

- Advertisement -