| Author |
Topic |
|
jhermiz
3564 Posts |
Posted - 2007-06-18 : 15:57:52
|
| Hoping to do this away from the client side...hopefully its a good idea. Basically I allow a user to select a start date and an end date to log some vacation days. What I am looking to get back is all days including and between StartDate and EndDate but excluding weekends ?Was wondering if anyone had a procedure where I could pull this information.For instance if I pass:@StartDate = 6/3/2007@EndDate = 6/12/2007It would return 6/4/2007, 6/5/07, 6/6/07, 6/7/07, 6/8/07, 6/11/07, 6/12/07.Should not include the 3rd and the 10 since those are Sundays and should not include the 9th since that is a saturday.Thanks,JonProgrammers HowTo's -- [url]http://jhermiz.googlepages.com[/url] |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-06-18 : 16:05:03
|
Perhaps there's an easier way but this can give you an idea:Declare @startdate datetime, @Enddate datetimeselect @StartDate = '6/03/2007' ,@EndDate = '6/12/2007' select @startdate , @Enddate , datename(weekday, @startdate ) Declare @t table (col1 datetime, col2 varchar(30))While @startdate <= @Enddate begin insert into @t select @startdate , datename(weekday, @startdate ) Set @startdate = Dateadd(dd, 1, @startdate ) end select * from @t Where col2 not in ('Sunday', 'Saturday')Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-06-18 : 16:13:36
|
| I would simply create a calendar table, mark weekend days as such, and also mark holidays and anything else you need. Create it once with a loop, make it all indexed and nice, go through and update holidays (or create a quick and easy front end for someone else to maintain it) and then it is a simple SELECT with a simple WHERE clause to get whatever days you need. Calendar tables make things so much easier and quicker.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-06-18 : 16:14:36
|
quote: Originally posted by jsmith8858 I would simply create a calendar table, mark weekend days as such, and also mark holidays and anything else you need. Create it once with a loop, make it all indexed and nice, go through and update holidays (or create a quick and easy front end for someone else to maintain it) and then it is a simple SELECT with a simple WHERE clause to get whatever days you need. Calendar tables make things so much easier and quicker.- Jeffhttp://weblogs.sqlteam.com/JeffS
Yep. That would be the ideal way...Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-06-18 : 21:50:40
|
| [code]declare @start_date datetimedeclare @end_date datetimeselect @start_date = '20070603'select @end_date = '20070612'select [DATE]from -- Function F_TABLE_DATE available here: -- http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519 dbo.F_TABLE_DATE ( @start_date, @end_date )where [DAY_OF_WEEK] between 2 and 6order by [DATE][/code]CODO ERGO SUM |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-06-19 : 05:04:11
|
Even though your best option speed wise will be to have a calander table, I just wanted to illustrate how a recursive query can still be used efficiently. Heres a fairly lightweight query that should be able to quickly return what you want. declare @StartDate datetime,@EndDate datetimeset @StartDate = '12/01/2006'set @EndDate = '6/01/2007'Declare @HolidayTable table(Date datetime)Insert into @HolidayTable(Date)select('12/24/2006') --xmas eveUnion allselect('12/25/2006') --xmasbeginwith TB1(d) as( Select @StartDate Union all Select Dateadd(dd,1,d) from TB1 a where a.d <= @EndDate ) select * from TB1 a where not exists (Select Date from @Holidaytable aa where aa.Date = a.d) and Datepart(dw,a.D) between 2 and 6 OPTION (MAXRECURSION 1000)end |
 |
|
|
jhermiz
3564 Posts |
Posted - 2007-06-19 : 09:27:26
|
quote: Originally posted by Michael Valentine Jones
declare @start_date datetimedeclare @end_date datetimeselect @start_date = '20070603'select @end_date = '20070612'select [DATE]from -- Function F_TABLE_DATE available here: -- http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519 dbo.F_TABLE_DATE ( @start_date, @end_date )where [DAY_OF_WEEK] between 2 and 6order by [DATE] CODO ERGO SUM
I too prefer a calendar and do agree that it would make a lot of sense but we have no one to maintain this...and I cant believe some of the holidays we have here!!!. MVJ this will definately do the trick, very nice btw..cant believe I hadn't seen this before!Thanks,JonProgrammers HowTo's -- [url]http://jhermiz.googlepages.com[/url] |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-06-19 : 14:00:44
|
| This is just my opionion,but MVJ function is by far the best and most inclusive function I have yet to see for creating dates. If you were going to create a calander table that would be the function to use. However if you are planning on running this on the fly each time(unless you modify his function significantly), the query I posted should be quicker(It is only returning 1 column and has very little calculations to perform). In the example I posted I illustrated how anyone can simply add a date to the Holiday table(IN YOUR DB THIS WOULD BE A PERMANT TABLE that was referanced rather then a variable). |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-06-19 : 15:41:27
|
| I agree that it would be better to have a permanent data table and the code I included is mostly to illustrate how it can be used. However, function F_TABLE_DATE will probably perform OK for applications that only need to make occasional use of a Date table. I tested with the 2006-12-01 to 2007-06-12 date range and had an elapsed time of 17 milliseconds.CODO ERGO SUM |
 |
|
|
jhermiz
3564 Posts |
Posted - 2007-06-19 : 15:48:08
|
quote: Originally posted by Michael Valentine Jones I agree that it would be better to have a permanent data table and the code I included is mostly to illustrate how it can be used. However, function F_TABLE_DATE will probably perform OK for applications that only need to make occasional use of a Date table. I tested with the 2006-12-01 to 2007-06-12 date range and had an elapsed time of 17 milliseconds.CODO ERGO SUM
Should work for me, thanks again folks.Programmers HowTo's -- [url]http://jhermiz.googlepages.com[/url] |
 |
|
|
jhermiz
3564 Posts |
Posted - 2007-06-26 : 23:23:15
|
quote: Originally posted by jhermiz
quote: Originally posted by Michael Valentine Jones I agree that it would be better to have a permanent data table and the code I included is mostly to illustrate how it can be used. However, function F_TABLE_DATE will probably perform OK for applications that only need to make occasional use of a Date table. I tested with the 2006-12-01 to 2007-06-12 date range and had an elapsed time of 17 milliseconds.CODO ERGO SUM
Should work for me, thanks again folks.Programmers HowTo's -- [url]http://jhermiz.googlepages.com[/url]
Sorry this is a late super late reply :-p. Just wanted to say Thanks MVJ that worked a treat. Was able to utilize various parts of that function, and it made my job a lot simpler.Just wanted to give you a shout out ! Programmers HowTo's -- [url]http://jhermiz.googlepages.com[/url] |
 |
|
|
|