Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2005, 10:19 PM
Registered User
 
Join Date: Mar 2005
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

 
Old March 21st, 2005, 01:37 AM
Friend of Wrox
 
Join Date: Nov 2003
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.
 
Old March 21st, 2005, 01:25 PM
Registered User
 
Join Date: Mar 2005
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

 
Old March 21st, 2005, 02:37 PM
Friend of Wrox
 
Join Date: Jan 2004
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





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





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