Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Compare a Date to today in Business days?

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 excluded

Can someone please help..

Ray

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2007-03-07 : 06:33:32
use DATEDIFF function.
Syntax
DATEDIFF ( datepart , startdate , enddate )

i.e

select datediff(day, '01/01/2007', '01/02/2007')

Mahesh
Go to Top of Page

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.
Go to Top of Page

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 Larsson
Helsingborg, Sweden
Go to Top of Page

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 Sundays


SELECT Date_Booked, DATEDIFF([day], Date_Booked, GETDATE()) AS NumberOfDays
from table
Go to Top of Page

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 Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

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
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-07 : 09:16:47
Create a function out of it and use it

create function f_count_business_day(@start datetime, @end datetime)
returns int
begin
declare @cnt int

select @cnt = count(*)
from F_TABLE_DATE(@start, @end)
where WEEKDAY_NAME not in ('Sat', 'Sun')

return @cnt
end

SELECT Date_Booked, dbo.f_count_business_day(Date_Booked, GETDATE()) AS NumberOfDays
from table




KH

Go to Top of Page

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_DAYS
go
create function dbo.F_GET_WORK_DAYS
(
@START_DATE datetime,
@END_DATE datetime
)
returns int
as
/*
Find the total workdays (Monday to Friday) between two dates,
including the start and end date.
*/

begin

declare @LAST_WEEK_START datetime
declare @WHOLE_WEEKS int
declare @WHOLE_WEEK_WORKDAYS int
declare @LAST_WEEK_START_DW int
declare @LAST_WEEK_DAYS int
declare @LAST_WEEK_WORKDAYS int
declare @WORKDAYS int

set @WHOLE_WEEKS = (datediff(dd,@START_DATE,@END_DATE)+1)/7

set @WHOLE_WEEK_WORKDAYS = @WHOLE_WEEKS*5

set @LAST_WEEK_START = dateadd(dd,@WHOLE_WEEKS*7,@START_DATE)

set @LAST_WEEK_START_DW = datediff(dd,'17530101',@LAST_WEEK_START)%7

set @LAST_WEEK_DAYS = datediff(dd,@LAST_WEEK_START,@END_DATE)+1

set @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
end

set @WORKDAYS = @WHOLE_WEEK_WORKDAYS + @LAST_WEEK_WORKDAYS

return @WORKDAYS

end
go




CODO ERGO SUM
Go to Top of Page

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

Go to Top of Page

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
Go to Top of Page

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

Go to Top of Page

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_diff
from tbl11_SWC_Open_Service_Orders
order by date_booked desc

results

Date_Booked BusinessDays_usingFunct FullDays_using_Date_diff

2007-03-09 2 3
2007-03-09 2 3
2007-03-09 2 3
2007-03-09 2 3
2007-03-05 6 7
2007-03-02 7 10
2007-03-02 7 10
2007-03-02 7 10
2007-03-02 7 10
2007-03-01 8 11
Go to Top of Page

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_diff
from tbl11_SWC_Open_Service_Orders
order by date_booked desc

results

Date_Booked BusinessDays_usingFunct FullDays_using_Date_diff

2007-03-09 2 3
2007-03-09 2 3
2007-03-09 2 3
2007-03-09 2 3
2007-03-05 6 7
2007-03-02 7 10
2007-03-02 7 10
2007-03-02 7 10
2007-03-02 7 10
2007-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
Go to Top of Page

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 ?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-12 : 21:44:19
You will have to define the Working hour. There was a similar thread on this recently. Try to do a search for it.

[EDIT]
It found me instead.
Take a look here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74645


KH

Go to Top of Page
   

- Advertisement -