Author |
Topic |
rwaldron
Posting Yak Master
131 Posts |
Posted - 2007-03-07 : 05:58:14
|
Hi all,I have a datetime field in an SQL DB that I need to compare to todays date in order to give me a number of days between the 2,But I would also like the number of days in question to be business days.. ie: Saturdays and Sundays excludedCan someone please help..Ray |
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2007-03-07 : 06:33:32
|
use DATEDIFF function.SyntaxDATEDIFF ( datepart , startdate , enddate ) i.eselect datediff(day, '01/01/2007', '01/02/2007')Mahesh |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2007-03-07 : 06:48:32
|
Mahesh....not quite....that counts all days...including sat+sun.Ray....search here for business/ weekends...this topic has come up before, with appropriate solutions posted. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-07 : 07:05:11
|
select count(*) from f_TABLE_DATE('20070101', '20071231') where weekday not in ('sat', 'sun')Peter LarssonHelsingborg, Sweden |
|
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2007-03-07 : 07:07:43
|
Thx so much for the reply guys, nearly there.I have used this to get the number of days ..but what about just counting business days ??ie: exclude Saturdays and SundaysSELECT Date_Booked, DATEDIFF([day], Date_Booked, GETDATE()) AS NumberOfDaysfrom table |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-07 : 07:21:29
|
Have you seen Peter's reply?That should satisfy your requirement. For definition of F_TABLE_DATE, see this: [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519&SearchTerms=f_table_date[/url]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2007-03-07 : 07:49:47
|
Thx guys,So I need to run this function in order to get business days.But will this work with my 2 paramaters, Date_Booked, GETDATE() instead of specific dates ?Thx,It's just that I haven't had to use a function like this before..Ray |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 09:16:47
|
Create a function out of it and use itcreate function f_count_business_day(@start datetime, @end datetime)returns intbegin declare @cnt int select @cnt = count(*) from F_TABLE_DATE(@start, @end) where WEEKDAY_NAME not in ('Sat', 'Sun') return @cntendSELECT Date_Booked, dbo.f_count_business_day(Date_Booked, GETDATE()) AS NumberOfDaysfrom table KH |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-03-07 : 14:12:08
|
Using the F_TABLE_DATE function will produce correct results, but will be very slow if you have a lot of date pairs to do. The function below will calculate it directly, and is much faster.drop function dbo.F_GET_WORK_DAYSgocreate function dbo.F_GET_WORK_DAYS ( @START_DATE datetime, @END_DATE datetime )returns intas/*Find the total workdays (Monday to Friday) between two dates,including the start and end date.*/begindeclare @LAST_WEEK_START datetimedeclare @WHOLE_WEEKS intdeclare @WHOLE_WEEK_WORKDAYS intdeclare @LAST_WEEK_START_DW intdeclare @LAST_WEEK_DAYS intdeclare @LAST_WEEK_WORKDAYS intdeclare @WORKDAYS intset @WHOLE_WEEKS = (datediff(dd,@START_DATE,@END_DATE)+1)/7set @WHOLE_WEEK_WORKDAYS = @WHOLE_WEEKS*5set @LAST_WEEK_START = dateadd(dd,@WHOLE_WEEKS*7,@START_DATE)set @LAST_WEEK_START_DW = datediff(dd,'17530101',@LAST_WEEK_START)%7set @LAST_WEEK_DAYS = datediff(dd,@LAST_WEEK_START,@END_DATE)+1set @LAST_WEEK_WORKDAYS = @LAST_WEEK_DAYS - case when @LAST_WEEK_DAYS = 0 then 0 when @LAST_WEEK_START_DW = 0 and @LAST_WEEK_DAYS > 6 then 2 when @LAST_WEEK_START_DW = 0 and @LAST_WEEK_DAYS > 5 then 1 when @LAST_WEEK_START_DW = 0 then 0 when @LAST_WEEK_START_DW = 1 and @LAST_WEEK_DAYS > 5 then 2 when @LAST_WEEK_START_DW = 1 and @LAST_WEEK_DAYS > 4 then 1 when @LAST_WEEK_START_DW = 1 then 0 when @LAST_WEEK_START_DW = 2 and @LAST_WEEK_DAYS > 4 then 2 when @LAST_WEEK_START_DW = 2 and @LAST_WEEK_DAYS > 3 then 1 when @LAST_WEEK_START_DW = 2 then 0 when @LAST_WEEK_START_DW = 3 and @LAST_WEEK_DAYS > 3 then 2 when @LAST_WEEK_START_DW = 3 and @LAST_WEEK_DAYS > 2 then 1 when @LAST_WEEK_START_DW = 3 then 0 when @LAST_WEEK_START_DW = 4 and @LAST_WEEK_DAYS > 2 then 2 when @LAST_WEEK_START_DW = 4 and @LAST_WEEK_DAYS > 1 then 1 when @LAST_WEEK_START_DW = 4 then 0 when @LAST_WEEK_START_DW = 5 and @LAST_WEEK_DAYS > 1 then 2 when @LAST_WEEK_START_DW = 5 and @LAST_WEEK_DAYS > 0 then 1 when @LAST_WEEK_START_DW = 5 then 0 when @LAST_WEEK_START_DW = 6 and @LAST_WEEK_DAYS > 6 then 2 else 1 endset @WORKDAYS = @WHOLE_WEEK_WORKDAYS + @LAST_WEEK_WORKDAYSreturn @WORKDAYSendgo CODO ERGO SUM |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 17:05:00
|
So i assume the F_GET_WORK_DAYS will also be to script lib ? KH |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-03-07 : 17:36:12
|
quote: Originally posted by khtan So i assume the F_GET_WORK_DAYS will also be to script lib ? KH
Maybe, that was just a first draft.I think I can eliminate the big CASE at the end with a formula if I give it some thought.Then there would be the matching function for finding the end date when you know the start date and number of working days.Or finding the start date when you know the end date and the number of working days...CODO ERGO SUM |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 20:21:32
|
Thank you for another great function. Maybe if we can also pass in a csv string to specify what are the non-working days like 'Sat,Sun' or maybe 'Fri,Sat' ? KH |
|
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2007-03-12 : 06:50:13
|
Hi Guys ths for your help so far.I have now used the finction F_GET_WORK_DAYS in the following query.I have also included a datediff function just to compare the results.Is F_GET_WORK_DAYS Inclusive of today ? Results seem to be 1 day out.Query is SELECT Date_Booked, dbo.f_get_work_days(Date_Booked, GETDATE()) AS BusinessDays_usingFunct,DATEDIFF([day], Date_Booked, GETDATE()) AS FullDays_using_Date_difffrom tbl11_SWC_Open_Service_Ordersorder by date_booked descresults Date_Booked BusinessDays_usingFunct FullDays_using_Date_diff 2007-03-09 2 32007-03-09 2 32007-03-09 2 32007-03-09 2 32007-03-05 6 72007-03-02 7 102007-03-02 7 102007-03-02 7 102007-03-02 7 102007-03-01 8 11 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-03-12 : 10:24:34
|
quote: Originally posted by rwaldron Hi Guys ths for your help so far.I have now used the finction F_GET_WORK_DAYS in the following query.I have also included a datediff function just to compare the results.Is F_GET_WORK_DAYS Inclusive of today ? Results seem to be 1 day out.Query is SELECT Date_Booked, dbo.f_get_work_days(Date_Booked, GETDATE()) AS BusinessDays_usingFunct,DATEDIFF([day], Date_Booked, GETDATE()) AS FullDays_using_Date_difffrom tbl11_SWC_Open_Service_Ordersorder by date_booked descresults Date_Booked BusinessDays_usingFunct FullDays_using_Date_diff 2007-03-09 2 32007-03-09 2 32007-03-09 2 32007-03-09 2 32007-03-05 6 72007-03-02 7 102007-03-02 7 102007-03-02 7 102007-03-02 7 102007-03-01 8 11
Not sure what you mean by "Results seem to be 1 day out"F_GET_WORK_DAYS counts both both @START_DATE and @END_DATE, if they are working days.CODO ERGO SUM |
|
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2007-03-12 : 11:50:19
|
thx for the reply Guys..Function is working correctly.Its the datediff function that I was using that is wrong.Just another quick question.Is there a way to use the function to return Days & Hours.Eg: Instead of say 6days between 2 dates. To actually return 5 Days 6 Hours etc ? |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|