Wrox Programmer Forums
|
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 July 10th, 2006, 08:40 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finiding Working days

Hi,
I want to find the count of all the Saturdays & Sundays within a date range stored in 2 columns startdate & enddate of a table called semesterMaster.

Can the same be done for the days of a month. Here no table would be used.

Can this be done with the help of SQL query.???

Pls help
 
Old July 10th, 2006, 11:19 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi,

There's at least two ways that I know of to do this. One is to generate a calendar table which creates dates in a table. Then you can select the datepart and count for the range that you want.

Another way to do this is to use Peso's Date Sequence Generator function:

http://p2p.wrox.com/topic.asp?TOPIC_...archTerms=peso

And then use the query:

SELECT count(DS.Value) FROM dbo.fnSeqDates('6/30/2006', '7/30/2006') DS WHERE DATEPART(dw, DS.Value) = 6 OR DATEPART(dw, DS.Value) = 7

The only problem I've seen with the sequence generator is that if you want to count all of the days between certain dates it seems that you have to subtract one day from the first date or it won't include the start day. As in this query I used the last day of June to start counting Saturday's and Sunday's so that it would include the Saturday on '7/1/2006'.

I'm sure they are many other ways to do this.

Richard






 
Old July 10th, 2006, 11:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

If it's at all possible, I recommend that you construct a calendar table. These are very easy to make, you can generate them years in advance so you don't have to worry about them much, and if you include an additional indicator or two in the calendar table you can indicate which days are business days, holidays, etc.

Having such a calendar makes calculations such as what the OP needs really quite simple. It also makes the next step - a business day calculation, also quite simple and fast.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old July 10th, 2006, 01:51 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

rstelma and arnabghosh, the DATEPART(dw, value) depends on the SET DATEFIRST setting in SQL Server.

If I run this code on my computer
Code:
SELECT value, DATEPART(dw, value) FROM fnSeqDates('7/1/2006', '7/31/2006')
I get this result
Code:
2006-07-01 00:00:00.000    7
2006-07-02 00:00:00.000    1
2006-07-03 00:00:00.000    2
2006-07-04 00:00:00.000    3
2006-07-05 00:00:00.000    4
2006-07-06 00:00:00.000    5
2006-07-07 00:00:00.000    6
2006-07-08 00:00:00.000    7
2006-07-09 00:00:00.000    1
2006-07-10 00:00:00.000    2
2006-07-11 00:00:00.000    3
2006-07-12 00:00:00.000    4
2006-07-13 00:00:00.000    5
2006-07-14 00:00:00.000    6
2006-07-15 00:00:00.000    7
2006-07-16 00:00:00.000    1
2006-07-17 00:00:00.000    2
2006-07-18 00:00:00.000    3
2006-07-19 00:00:00.000    4
2006-07-20 00:00:00.000    5
2006-07-21 00:00:00.000    6
2006-07-22 00:00:00.000    7
2006-07-23 00:00:00.000    1
2006-07-24 00:00:00.000    2
2006-07-25 00:00:00.000    3
2006-07-26 00:00:00.000    4
2006-07-27 00:00:00.000    5
2006-07-28 00:00:00.000    6
2006-07-29 00:00:00.000    7
2006-07-30 00:00:00.000    1
2006-07-31 00:00:00.000    2
So for me Saturday is 7 and Sunday is 1.

So if I write
Code:
SELECT count(*) FROM fnSeqDates('7/1/2006', '7/31/2006') where DATEPART(dw, value) in (1, 7)
I get 10 as the expected result.
 
Old July 10th, 2006, 04:00 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Peso,

I'm a ding dong. You're absolutely right. I get the same results as you do when I select the correct days, 1 and 7.

Thank you!

Richard

 
Old July 11th, 2006, 02:18 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peso for your great help!!!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
days-from-duration Dinghus XSLT 1 November 21st, 2007 04:05 AM
Last 7 Days Record prasanta2expert Access 1 October 23rd, 2006 05:38 AM
Days in a Month jmss66 Classic ASP Basics 3 April 11th, 2005 05:57 PM
No of days anandham SQL Server 2000 2 March 21st, 2005 01:20 AM
Last records from 7 days only jemacc SQL Server 2000 3 September 28th, 2004 11:59 PM





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