yes
Main Topics
Browse All TopicsThis query produces 21 days for @TotalPostingDays ...the days left after pulling out weekend days (sat and sun) and holidays. What I want to do is instead of pulling the total amount as a Count for
@TotalPostingDays I want to actually insert the actual dates that the @TotalPostingDays found into a table
DECLARE @TotalDaysInMonth int,
@today datetime,
@TotalWeekendDays int,
@TotalHolidaysThisMonth int,
@TotalPostingDays int
SET @today = GETDATE()
-- TOTAL DAYS THIS MONTH
SET @TotalDaysInMonth = CASE WHEN DatePart(mm,GetDate()) IN (1,3,5,7,8,10,12) THEN
31
ELSE
DateDiff(day,GetDate(),Dat
END
-- TOTAL HOLIDAYS THIS MONTH
SELECT @TotalHolidaysThisMonth = (SELECT COUNT(*) FROM Apex_ReportingServer.dbo.H
WHERE HolidayDate BETWEEN (DATEADD(DAY, -DATEPART(DAY, @today) + 1, @today))
AND (DATEADD(DAY, -DATEPART(DAY, @today), DATEADD(MONTH, 1, @today))))
-- TOTAL # WEEKEND DAYS THIS MONTH
DECLARE @date DATETIME
SET @date = '20060101'
SELECT @TotalWeekendDays = 8 +
CASE WHEN ISDATE(CONVERT(CHAR(6), @date, 112) + '29') = 1 THEN
CASE WHEN DATENAME(WEEKDAY, CONVERT(CHAR(6), @date, 112) + '01') IN ('Saturday', 'Sunday')
THEN 1 ELSE 0 END ELSE 0 END +
CASE WHEN ISDATE(CONVERT(CHAR(6), @date, 112) + '30') = 1 THEN
CASE WHEN DATENAME(WEEKDAY, CONVERT(CHAR(6), @date, 112) + '02') IN ('Saturday', 'Sunday')
THEN 1 ELSE 0 END ELSE 0 END +
CASE WHEN ISDATE(CONVERT(CHAR(6), @date, 112) + '31') = 1 THEN
CASE WHEN DATENAME(WEEKDAY, CONVERT(CHAR(6), @date, 112) + '03') IN ('Saturday', 'Sunday')
THEN 1 ELSE 0 END ELSE 0 END
SELECT @TotalPostingDays = @TotalDaysInMonth - (@TotalHolidaysThisMonth + @TotalWeekendDays)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
This will get you started. This provides a list of Week Days for the current month. You should be able to modify it to fit what you need. I left it as a temporary table, but you'll probably want to make that a permanent table, depending on how often you want to run this process:
hth,
Landy
set nocount on
drop table #days
create table #days
(MonthDay datetime,
DayOfWeek int)
declare @Start_date datetime,
@End_date datetime,
@date datetime
set @Start_date ='1/1/' + cast(datepart(yy,getdate()
set @End_date ='12/31/' + cast(datepart(yy,getdate()
set @date = dateadd(dd,-1,@Start_date)
While @date < @End_date
Begin
SELECT @date = DATEADD(dd, 1,@date)
INSERT INTO #days
Values (@date, datepart(dw,@date))
end
select MonthDay from #days where Datepart(dw, MonthDay) not in (1,7) and DatePart(mm,MonthDay) = DatePart(mm,GetDate())
Sorry, I left out the Holiday part.
set nocount on
drop table #days
create table #days
(MonthDay datetime,
DayOfWeek int,
Holiday bit)
declare @Start_date datetime,
@End_date datetime,
@date datetime
set @Start_date ='1/1/' + cast(datepart(yy,getdate()
set @End_date ='12/31/' + cast(datepart(yy,getdate()
set @date = dateadd(dd,-1,@Start_date)
--Load table with all of this year's dates and dayofweek
While @date < @End_date
Begin
SELECT @date = DATEADD(dd, 1,@date)
INSERT INTO #days
Values (@date, datepart(dw,@date), 0)
end
--Set the holidays
UPDATE #days
SET Holiday = 1
From Apex_ReportingServer.dbo.H
INNER JOIN #days d on d.MonthDate = h.HolidayDate
WHERE
DatePart(yy,h.HolidayDate)
--Get this month's work days
SELECT MonthDay
FROM #days
WHERE Datepart(dw, MonthDay) not in (1,7)
and DatePart(mm,MonthDay) = DatePart(mm,GetDate())
and Holiday <> 1
So you actualy want to insert all working days into one column for given month, correct?
if yes tis should do the job
SET NOCOUNT ON
Declare @Holidays table (HolidayDate datetime) -- this is temp table for holidays
Insert @Holidays values ('2006/1/1') -- just for test
Insert @Holidays values ('2006/1/2')
Declare @PostingDays table (PostingDay datetime) -- in this table we are going to insert dates
Declare @Today datetime -- is is your parameter date
select @Today= getdate()
Declare @CurrentDay datetime
Select @CurrentDay = cast( cast(year(@Today) as varchar(4)) +'/'+cast(month(@Today) as varchar(2)) +'/1' as datetime)
WHILE month(@CurrentDay)=month(@
BEGIN
IF NOT DATEPART(weekday, @CurrentDay) IN (1,7)
IF NOT EXISTS (SELECT * FROM @Holidays WHERE HolidayDate= @CurrentDay)
Insert into @PostingDays Values(@CurrentDay)
SELECT @CurrentDay= DATEADD(Day,1,@CurrentDay)
END
Select * FROM @PostingDays -- Check the results
Business Accounts
Answer for Membership
by: acperkinsPosted on 2006-01-30 at 16:51:25ID: 15829741
Is this what you mean:
INSERT MyTableName(Col1, Col2, ..., TotalPostingDays) VALUES (@Value1, @Value2, ..., @TotalPostingDays)