473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calculating the Weekend Days(Saturaday,Sunday)

Manikgisl
HI,

I have two different dates

26-12-2008 to 26-01-2009
these are the two dates ..

i want calculate number of saturday and sundays in between two date


anybody having an idea....
Pls help me

With Regards,

Mani
Mar 12 '09 #1
8 11977
ck9663
2,878 Expert 2GB
What's the biggest (in days) the possible range of your FromDate and ToDate?


-- CK
Mar 12 '09 #2
Jibran
30
You can do a count() or have a countter for each type of day for the number of entries that return Saturday and Sunday using DATENAME() function.
Mar 12 '09 #3
hi
I Having the 26 27 28 29 30 as Fields in our tables ..
i want marked 'O' in the Saturday and Sunday Coloumn\


Thanks In advance

With regards
Mani
Mar 13 '09 #4
Delerna
1,134 Expert 1GB
@Manikgisl

Hi Mani
This is purely a matematics exercise
Here is one method (I think it is right) :-}

Expand|Select|Wrap|Line Numbers
  1. declare @Start datetime,@End Datetime
  2. set @Start='2009-03-14'
  3. set @End='2009-03-29'
  4.  
  5.  
  6. select WkDay,
  7.        TotDays,
  8.        DaysTillNextSat,
  9.        DaysTillNextSun,
  10.        (7-DaysTillNextSat)/7+(TotDays-DaysTillNextSat)/7+case when DaysTillNextSat<=totdays and DaysTillNextSat<>0 then 1 else 0 end as NumSats,
  11.        (8-DaysTillNextSun)/7+(TotDays-DaysTillNextSun)/7 as NumSuns
  12. from
  13. (   select WkDay,TotDays,
  14.               7-WkDay as DaysTillNextSat,case when 7-WkDay+1=7 then 0 else 7-WkDay+1 end as DaysTillNextSun
  15.     from(   select datepart(dw,@Start) as WkDay,
  16.                        datediff(dd,@Start,@End)as TotDays
  17.         )a
  18. )a
  19.  
I have tried to write the query so you can follow my logic (hopefully)
rather than giving you a concice query

Take the query, analyse it and reduce it so that it returns only the fields you need.
When done, make a user defined function out of it.

I hope it helps you

If anyone can come up with a mathematically neater way then I for one would like to see it

Regards
Mar 18 '09 #5
Delerna
1,134 Expert 1GB
I thought I might try and explain this line a little bit
Expand|Select|Wrap|Line Numbers
  1. (7-DaysTillNextSat)/7+(TotDays-DaysTillNextSat)/7+case when DaysTillNextSat<=totdays and DaysTillNextSat<>0 then 1 else 0 end as NumSats
  2.  
TotDays is the number of days in the date range

(7-DaysTillNextSat)/7 will be 1 if @StartDate is a saturday otherwise 0

This bit
(TotDays-DaysTillNextSat)/7
gives the number of saturdays (one per week)
over and above @StartDate being a saturday

However, if
Totdays-DaysTillNextSat
is less than 7 then it will be 0
but there still might be a saturday somwere in there

so this bit
case when DaysTillNextSat<=totdays and DaysTillNextSat<>0 then 1 else 0 end
corrects the result in that case

The DaysTillNextSat<>0 criteria is there because
@Startdate being saturday has already been covered
Mar 18 '09 #6
this function is close but seems not to work 100% ( at least on my end)
Please try these DateTime Stamps

2009-04-03 11:42:36 (yyyy-mm-dd)
2009-04-06 09:16:49

It should come up with 1 Sat and 1 Sun, but for me it only return 1 Sat and 0 Sun.
Apr 14 '09 #7
ck9663
2,878 Expert 2GB
Let me try.......

This query will return all the days from your start date to end date....

Expand|Select|Wrap|Line Numbers
  1. declare @startdate as datetime, @enddate as datetime
  2.  
  3. select  @startdate = '12-26-2008', @enddate = '01-26-2009'
  4.  
  5. select    
  6.    dateadd(dd,days,@startdate), datename(dw,dateadd(dd,days,@startdate))
  7. from
  8. (select top 365 colorder - 1 as days from master..syscolumns where id = -519536829 order by colorder) x
  9. where datediff(dd,dateadd(dd,days,@startdate),@enddate) >= 0
  10.  
Here's the code that will count all weekends..

Expand|Select|Wrap|Line Numbers
  1.  
  2. select
  3.    sum(       
  4.    case 
  5.       when datename(dw,dateadd(dd,days,@startdate)) in ('SATURDAY', 'SUNDAY') then 1
  6.       else 0
  7.    end)
  8. from
  9. (select top 365 colorder - 1 as days from master..syscolumns where id = -519536829 order by colorder) x
  10. where datediff(dd,dateadd(dd,days,@startdate),@enddate) >= 0
  11.  
  12.  
so is this...

Expand|Select|Wrap|Line Numbers
  1.  
  2. select
  3.    count(*)
  4. from
  5. (select top 365 colorder - 1 as days from master..syscolumns where id = -519536829 order by colorder) x
  6. where datediff(dd,dateadd(dd,days,@startdate),@enddate) >= 0 and datename(dw,dateadd(dd,days,@startdate)) in ('SATURDAY', 'SUNDAY')
  7.  
  8.  
All queries will run if your dates are one year apart. If you need more, adjust the TOP 365 portion.


--- CK
Apr 15 '09 #8
Delerna
1,134 Expert 1GB
Yea, you're right
In my explanation of the query I said
However, if
Totdays-DaysTillNextSat
is less than 7 then it will be 0
but there still might be a saturday somewere in there

so this bit
case when DaysTillNextSat<=totdays and DaysTillNextSat<>0 then 1 else 0 end
corrects the result in that case

The DaysTillNextSat<>0 criteria is there because
@Startdate being saturday has already been covered
I didn't do the same thing for sunday
so in the above explanation substitute sunday for saturday
and add the code being described to the NumSuns calculation

specifically, this bit

+case when DaysTillNextSun<=totdays and DaysTillNextSun<>0 then 1 else 0 end


Expand|Select|Wrap|Line Numbers
  1. declare @Start datetime,@End Datetime 
  2. set @Start='2009-04-3 11:42:36' 
  3. set @End='2009-04-06 09:16:49' 
  4.  
  5.  
  6. select WkDay,
  7.        TotDays, 
  8.        DaysTillNextSat, 
  9.        DaysTillNextSun, 
  10.        (7-DaysTillNextSat)/7+(TotDays-DaysTillNextSat)/7+case when DaysTillNextSat<=totdays and DaysTillNextSat<>0 then 1 else 0 end as NumSats, 
  11.        (8-DaysTillNextSun)/7+(TotDays-DaysTillNextSun)/7+case when DaysTillNextSun<=totdays and DaysTillNextSun<>0 then 1 else 0 end as NumSuns 
  12. from 
  13. (
  14.    select WkDay,TotDays, 
  15.               7-WkDay as DaysTillNextSat,case when 7-WkDay+1=7 then 0 else 7-WkDay+1 end as DaysTillNextSun 
  16.     from(   select datepart(dw,@Start) as WkDay, 
  17.                        datediff(dd,@Start,@End)as TotDays 
  18.         )a 
  19. )a 
  20.  
Apr 21 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Eric Linders | last post by:
Hello, Today is Thursday, August 18, 2004. I would like to have a variable that stores the date this coming Sunday (even if today happened to be Sunday) in YYY-MM-DD format. I also need a...
38
by: | last post by:
I have a script... ----- <SCRIPT language="JavaScript" type="text/javascript"> <!-- function makeArray() { for (i = 0; i<makeArray.arguments.length; i++) this = makeArray.arguments; } ...
7
by: AdityaK | last post by:
Is there a way to find Weekend date for a given date using a DB2 query? I see that there are scalar functions such as DAY, DAYOFMONTH, DAYOFWEEK,etc are available...but couldn't find one to get a...
6
by: Zee | last post by:
I need help using a query to calculate or subtract values in a row tblCalRows RowID RowValue 1 A 2 B 3 C 4.... D
1
by: rock72 | last post by:
I am developing a application using this fields as required by the company. 1. Date Login 2. Time IN 3. Time OUT My question is how to produce the number of days with 1 entry of Date is...
1
by: rock72 | last post by:
Hello Everyone, Can you tell me how to produce a query that when Login Date (entry) is Sunday. First, check if the Sunday Date occur on 7th day of his duty. Example 1: Login Date no. of...
7
by: bojan.pikl | last post by:
Hi, I am making a calendar. It is costum made and I would like to have the ability to choose the first day (Monday or Sunday). I know for the firstDayOfWeek, but I can't change it. What should I...
0
by: Trent Nelson | last post by:
Just a friendly reminder that this weekend is the Python sprint weekend! Look forward to seeing everyone on #python-dev irc.freenode.net over the course of the weekend! Trent. On 16 Apr,...
8
by: =?Utf-8?B?QWw=?= | last post by:
I am working in vb2005. how can I calculate business days (not including holidays and weekends) between 2 dates? thanks Al
4
Manikgisl
by: Manikgisl | last post by:
HI, I have two different dates 26-12-2008 to 26-01-2009 these are the two dates .. i want calculate number of saturday and sundays in between two dates
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.