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
8 11977
What's the biggest (in days) the possible range of your FromDate and ToDate?
-- CK
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.
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
@Manikgisl
Hi Mani
This is purely a matematics exercise
Here is one method (I think it is right) :-} -
declare @Start datetime,@End Datetime
-
set @Start='2009-03-14'
-
set @End='2009-03-29'
-
-
-
select WkDay,
-
TotDays,
-
DaysTillNextSat,
-
DaysTillNextSun,
-
(7-DaysTillNextSat)/7+(TotDays-DaysTillNextSat)/7+case when DaysTillNextSat<=totdays and DaysTillNextSat<>0 then 1 else 0 end as NumSats,
-
(8-DaysTillNextSun)/7+(TotDays-DaysTillNextSun)/7 as NumSuns
-
from
-
( select WkDay,TotDays,
-
7-WkDay as DaysTillNextSat,case when 7-WkDay+1=7 then 0 else 7-WkDay+1 end as DaysTillNextSun
-
from( select datepart(dw,@Start) as WkDay,
-
datediff(dd,@Start,@End)as TotDays
-
)a
-
)a
-
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
I thought I might try and explain this line a little bit -
(7-DaysTillNextSat)/7+(TotDays-DaysTillNextSat)/7+case when DaysTillNextSat<=totdays and DaysTillNextSat<>0 then 1 else 0 end as NumSats
-
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
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.
Let me try.......
This query will return all the days from your start date to end date.... -
declare @startdate as datetime, @enddate as datetime
-
-
select @startdate = '12-26-2008', @enddate = '01-26-2009'
-
-
select
-
dateadd(dd,days,@startdate), datename(dw,dateadd(dd,days,@startdate))
-
from
-
(select top 365 colorder - 1 as days from master..syscolumns where id = -519536829 order by colorder) x
-
where datediff(dd,dateadd(dd,days,@startdate),@enddate) >= 0
-
Here's the code that will count all weekends.. -
-
select
-
sum(
-
case
-
when datename(dw,dateadd(dd,days,@startdate)) in ('SATURDAY', 'SUNDAY') then 1
-
else 0
-
end)
-
from
-
(select top 365 colorder - 1 as days from master..syscolumns where id = -519536829 order by colorder) x
-
where datediff(dd,dateadd(dd,days,@startdate),@enddate) >= 0
-
-
so is this... -
-
select
-
count(*)
-
from
-
(select top 365 colorder - 1 as days from master..syscolumns where id = -519536829 order by colorder) x
-
where datediff(dd,dateadd(dd,days,@startdate),@enddate) >= 0 and datename(dw,dateadd(dd,days,@startdate)) in ('SATURDAY', 'SUNDAY')
-
-
All queries will run if your dates are one year apart. If you need more, adjust the TOP 365 portion.
--- CK
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 -
declare @Start datetime,@End Datetime
-
set @Start='2009-04-3 11:42:36'
-
set @End='2009-04-06 09:16:49'
-
-
-
select WkDay,
-
TotDays,
-
DaysTillNextSat,
-
DaysTillNextSun,
-
(7-DaysTillNextSat)/7+(TotDays-DaysTillNextSat)/7+case when DaysTillNextSat<=totdays and DaysTillNextSat<>0 then 1 else 0 end as NumSats,
-
(8-DaysTillNextSun)/7+(TotDays-DaysTillNextSun)/7+case when DaysTillNextSun<=totdays and DaysTillNextSun<>0 then 1 else 0 end as NumSuns
-
from
-
(
-
select WkDay,TotDays,
-
7-WkDay as DaysTillNextSat,case when 7-WkDay+1=7 then 0 else 7-WkDay+1 end as DaysTillNextSun
-
from( select datepart(dw,@Start) as WkDay,
-
datediff(dd,@Start,@End)as TotDays
-
)a
-
)a
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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;
}
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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
|
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
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
| | |