| Author |
Topic |
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2009-12-09 : 18:50:13
|
| Find the nonholiday end of the month.last day of any month SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))but how to calculate the nonholiday last day of the month.so it has to skip weekends AND holidays ( select holiday_date from Holidays) Chandragupta Mourya |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2009-12-09 : 19:09:03
|
Not to appear snarky but...select max(DayOfMonth)from Calendarwhere CalYear = Year(GetDate())and CalMonth = Month(GetDate())and Holiday = 0 Having a Calendar table that holds this information is very helpful...frequently.=======================================Few things are harder to put up with than the annoyance of a good example. (Mark Twain) |
 |
|
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2009-12-10 : 13:20:19
|
| if we dont have calandar table, then ?Chandragupta Mourya |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
|
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2009-12-14 : 12:30:10
|
| already There is a holiday table, in use. So we cant afford a new calandar table for only one use.Chandragupta Mourya |
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2009-12-14 : 12:55:42
|
| calendar tables is all inclusive. holidays non holidays etc. it is worth investing in, Ryan is right you will not regret it. why do you have only a holiday table, now you are paying for that bad decision in that you are looking for a non holiday value.<><><><><><><><><><><><><><><><><><><><><><><><><>If you don't have the passion to help people, you have no passion |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2009-12-14 : 13:02:00
|
If you think there'll be only one use, you're not seeing the potential of a calendar table.To easy yourself in gently, you could use a generic calendar/date table function, and then join it with your holiday table.declare @Holidays table (holiday_date datetime)insert @Holidays select '20091225'union all select '20091231'--F_TABLE_DATE: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519select YEAR_MONTH, max(DATE) as LastWorkingDayOfMonthfrom dbo.F_TABLE_DATE('20060101', '20101231')where date not in (select holiday_date from @Holidays)group by YEAR_MONTHRyan Randall - Yak of all tradesSolutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2009-12-14 : 13:30:52
|
| I have a a function dbo.calcnextbusday( date, noofdaysto add) to calculate Next businessday . Can I use this function to calculate the last nonholiday business day of the monthChandragupta Mourya |
 |
|
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2009-12-14 : 14:49:28
|
| CREATE FUNCTION dbo.fnEndOfMonth()RETURNS DATETIMEASBEGIN DECLARE @n INTDECLARE @EndOfMonth DatetimeSET @n=-1SET @EndOfMonth=DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))WHILE ((DATENAME(dw, @EndOfMonth) in ('Saturday','Sunday'))OR convert( varchar,@EndOfMonth, 112) in (SELECT convert( varchar ,holiday_date,112) FROM dbo.Holidays_TABLE))BEGINSET @n=@n-1SET @EndOfMonth=DATEADD(d,@n,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))ENDRETURN @EndOfMonthENDChandragupta Mourya |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-12-14 : 15:10:23
|
quote: Originally posted by konark already There is a holiday table, in use. So we cant afford a new calandar table for only one use.Chandragupta Mourya
What "Holidays" do you wish to look forI mean, damn, these questions are so simpleIf you where a Nazi for example....ummmm never mindOr Dick Chenney...wait, that would be the same thing...I'm SorryBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2009-12-15 : 05:11:50
|
quote: calculate the last nonholiday business day of the month
declare @Holidays table (holiday_date datetime)insert @Holidays select '20091225'union all select '20091231'--F_TABLE_DATE: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519select YEAR_MONTH, max(DATE) as LastWorkingDayOfMonthfrom dbo.F_TABLE_DATE('20060101', '20101231')where date not in (select holiday_date from @Holidays) and WEEKDAY_NAME not in ('Sat', 'Sun')group by YEAR_MONTHSee how easy it is?Ryan Randall - Yak of all tradesSolutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|