SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Calculate 2 business days
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ph1long
Starting Member

16 Posts

Posted - 11/02/2012 :  12:30:11  Show Profile  Reply with Quote
Hi,

I have a Calendar table. I would like get 2 business days before Today's Date while skipping Holidays and weekeends.

For example:

Example 1: New years 2013 falls on a Tuesday. If it was Thursday (1/3), then my starting date would be Monday (12/31).

Example 2: If it was Monday (12/31), then my starting date would be Thursday (12/27).

My Calendar table as been prepopulated with 30 years worth of dates and have flags to determine which ones are weekend and hollidays. The table has these fields:

dt: Date and time for 30 years
Y: year
D: Day
M: Month
Isholiday: (Is a holiday are not, flagged with 0 for "no" and 1 for "Yes")
IsWeekday: (Is a weekday? Flagged with 0 for "no" and 1 for "Yes" HolidayDescritption: Holiday Names

Please help.

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 11/02/2012 :  13:25:43  Show Profile  Reply with Quote
select top 2
	*
from
	MyCalendarTable
where
	-- After 10 days before today
	MyDate > dateadd(dd,-10,getdate) and
	-- Before today
	MyDate < dateadd(dd,datediff(dd,0,getdate()),0) and
	Isholiday = 0 and
	IsWeekday = 1
order by
	MyDate desc


CODO ERGO SUM

Edited by - Michael Valentine Jones on 11/02/2012 13:26:55
Go to Top of Page

ph1long
Starting Member

16 Posts

Posted - 11/02/2012 :  13:49:39  Show Profile  Reply with Quote
If it Is Monday (12/31), the start date should be Thursday 12/27, but the above solution gives me Friday 12/28 and 12/27Friday is only one business day before Monday, not 2. Is there anyway to come back with only 1 correct result?

Thanks
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/02/2012 :  15:18:33  Show Profile  Reply with Quote
SELECT TOP 1 * FROM (
select top 2
	*
from
	MyCalendarTable
where
	-- After 10 days before today
	MyDate > dateadd(dd,-10,getdate) and
	-- Before today
	MyDate < dateadd(dd,datediff(dd,0,getdate()),0) and
	Isholiday = 0 and
	IsWeekday = 1
order by
	MyDate DESC
) s ORDER BY MyDate ASC
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 11/02/2012 :  16:06:28  Show Profile  Reply with Quote
quote:
Originally posted by ph1long

If it Is Monday (12/31), the start date should be Thursday 12/27, but the above solution gives me Friday 12/28 and 12/27Friday is only one business day before Monday, not 2. Is there anyway to come back with only 1 correct result?

Thanks



In your post, you said "I would like get 2 business days before Today's Date while skipping Holidays and weekends." so I gave you the two business days you asked for.

Plus the title of this thread is "Calculate 2 business days".

It's hard to read that as wanting anything other that 2 business days.





CODO ERGO SUM

Edited by - Michael Valentine Jones on 11/02/2012 16:09:53
Go to Top of Page

ph1long
Starting Member

16 Posts

Posted - 11/02/2012 :  17:15:16  Show Profile  Reply with Quote
Thank Michael and sunitabeck.

Sunitabeck's variation of Michael's query worked.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000