Thursday, March 22, 2012

Calculate weekend days between two dates.

How do I calculate the number of wends that are between two dates?
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!http://www.aspfaq.com/2519
http://www.aspfaq.com/
(Reverse address to reply.)
"skb" <nospamskberen@.msn.com> wrote in message
news:ezJye1qGFHA.3068@.tk2msftngp13.phx.gbl...
> How do I calculate the number of wends that are between two dates?
>
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!|||I dont think there is a direct function to do this.. but here is a way
around....(Please excause any syntex errors.. I am just writing off
the top of my head..)
-- ****************************************
****************
Declare @.startdate smalldatetime,
@.enddate smalldatetime,
@.tempdate datetime,
@.holiday smallint
set @.startdate = '01/01/2005' -- set to relevent start date
set @.enddate = '01/31/2005' -- set to relevent end date
set @.holiday = 0
Set @.tempdate = @.startdate
while @.tempdate <= @.enddate
begin
print convert(varchar, @.tempdate) + ' : ' + DateName (dw, @.tempdate )
if DateName (dw, @.tempdate ) = 'Saturday'
begin
print @.holiday
set @.holiday = @.holiday + 1
end
if DateName (dw, @.tempdate ) = 'sunday'
begin
print @.holiday
set @.holiday = @.holiday + 1
end
set @.tempdate = dateadd(d,1,@.tempdate)
end
select 'Total number of holidays between ' + convert (Varchar,
@.startdate) + ' to ' + convert (Varchar, @.enddate) + ' : ' + convert
(varchar, @.holiday)
-- ****************************************
****************|||If you only need wends remove the sunday check!!!!|||Build a calendar table with one column for the calendar data and other
columns to show whatever your business needs in the way of temporal
information. Do not try to calculate holidays in SQL -- Easter alone
requires too much math.
CREATE TABLE Calendar
(cal_date DATE NOT NULL PRIMARY KEY,
fiscal_year SMALLINT NOT NULL,
fiscal_month SMALLINT NOT NULL,
w_in_year SMALLINT NOT NULL, -- SQL server is not ISO standard
holiday SMALLINT NOT NULL
CHECK(holiday IN (0,1)),
day_in_year SMALLINT NOT NULL,
..);
A calendar table for US Secular holidays can be built from the data at
this website, so you will get the three-day wends:
http://www.smart.net/~mmontes/ushols.html|||Thanks for the help...For performance I think creating a table with
wend dates is the best.
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!

No comments:

Post a Comment