Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 20th, 2005, 10:19 PM
Registered User
 
Join Date: Mar 2005
Location: Fremont, CA, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help for Last 30 Business Days Query

Hi all,

     New to this forum...My client is asking me to create filter "Last 30 Business Days" so that they will use them in Business Objects Reports.When they use this filter,they should get last 30 business days data in the report.It should skip weekends...Means excluding weekends it should be 30 days...
         I am wondering how to create this query which recognizes weekends and skips them and take last 30 days...They may even ask for 90 days and so on...

Can someone help me to get this query in SQL Server ...

Thanks,
Nath

Reply With Quote
  #2 (permalink)  
Old March 21st, 2005, 01:37 AM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Try this. this uses a table as an example from the Northwind data base:

SELECT orderdate from orders where DATEDIFF(day, orderdate, getdate()) <=30 and datepart(dw, orderdate) not in (1,7) order by orderdate desc

getdate() = current date
1= sunday, 7 =saturday

Just modify to use your tables.
Reply With Quote
  #3 (permalink)  
Old March 21st, 2005, 01:25 PM
Registered User
 
Join Date: Mar 2005
Location: Fremont, CA, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi ,

  I tried with your query but it is seeing only weekends in last 30 days and skips them but that is not with my case.I need to get data for Last 30 business days.

Thanks,
Nath

Reply With Quote
  #4 (permalink)  
Old March 21st, 2005, 02:37 PM
Friend of Wrox
 
Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This UDF (User Defined Function) might come to your rescue...(Not my work..original author Jeff Smith I found it in one of the online forums)

Create function WorkDay(@CurrDate datetime, @DaysToAdd int)
returns datetime
as
begin
    declare @WeeksToAdd int;
    declare @dw int;

    set @WeeksToAdd = (@DaysToAdd / 5) * 7
    set @DaysToAdd = @DaysToAdd % 5

    set @CurrDate = DateAdd(dd,@WeeksToAdd + @DaysToAdd, @CurrDate)
    set @dw = datepart(dw,@CurrDate)

    if sign(@DaysToAdd) < 0
        begin
        if @dw = 1 set @CurrDate = @CurrDate - 2
        if @dw = 7 set @CurrDate = @CurrDate - 1
        end
    else
        begin
        if @dw = 1 set @CurrDate = @CurrDate + 1
        if @dw = 7 set @CurrDate = @CurrDate + 2
        end

return @CurrDAte
end

Note: The function assumes that the starting date you specify will be a week day.


DECLARE @LastBusinessDay Datetime
SELECT @LastBusinessDay = dbo.WorkDay(GetDATE(),-30)

It should return Feb 7 2005 12:00AM

Now modify your query, you might have to use stored procedure for this
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help to query for data by 30-mins interval sunny76 Access 2 June 19th, 2008 11:52 AM
Need to query dates after 90 days old melkin SQL Language 9 October 30th, 2007 08:03 AM
Debugging SOAPClient.30 in VB frugalsnow XML 2 August 15th, 2007 01:57 PM
Want Build Your Own Members Lists in 30 Days or L Ranjitha Need help with your homework? 0 July 12th, 2007 05:11 AM
Calculate Business Days - MS access 97 snoopy92211 Access VBA 0 October 13th, 2004 02:13 PM



All times are GMT -4. The time now is 07:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.