Reputation: 539
I need to find number of Sundays between two dates using mysql. I know how to do it using PHP But i need it to be calculated using mysql.
Upvotes: 7
Views: 13474
Reputation: 1991
SET @START_DATE = '2014-01-22';
SET @END_DATE = '2014-06-29';
SELECT
ROUND((
(unix_timestamp(@END_DATE) - unix_timestamp(@START_DATE) ) /(24*60*60)
-7+WEEKDAY(@START_DATE)-WEEKDAY(@END_DATE)
)/7)
+ if(WEEKDAY(@START_DATE) <= 6, 1, 0)
+ if(WEEKDAY(@END_DATE) >= 6, 1, 0) as Sundays;
Solution consist of 2 parts:
If you'll need to use it more than once you can wrap it into the function:
DROP function IF EXISTS `count_weekdays`;
DELIMITER $$
CREATE FUNCTION `count_weekdays` (startDate date, endDate date, wd int)
RETURNS INTEGER
BEGIN
RETURN ROUND((
(unix_timestamp(endDate) - unix_timestamp(startDate) ) /(24*60*60)
-7+WEEKDAY(startDate)-WEEKDAY(endDate)
)/7)
+ if(WEEKDAY(startDate) <= wd, 1, 0)
+ if(WEEKDAY(endDate) >= wd, 1, 0);
END$$
DELIMITER ;
Then you will be able to use it like this:
SET @START_DATE = '2018-07-03';
SET @END_DATE = '2018-07-28';
select
count_weekdays(@START_DATE, @END_DATE, 6) as Sundays,
count_weekdays(@START_DATE, @END_DATE, 5)
+ count_weekdays(@START_DATE, @END_DATE, 6) as weekends;
Where 6 ~ number of weekday stands for Sunday, 5 ~ Saturday.
Upvotes: 8
Reputation: 101
SELECT a.StartDate, a.EndDate,
(DAY(EndDate - StartDate) / 7)
+ iif(DAY(EndDate - StartDate)%7 + DATEPART(DW, StartDate) > 8 , 1, 0)
Sundays FROM
(SELECT GETDATE() StartDate, DATEADD(DAY, 11, GETDATE()) EndDate) a
This is useful for multiple start and end dates in a table. no need of variables and loops!
How it works: determines the number of full weeks between start and end date,
determines the days less than a week and add with the start day index(1,2,3,4,5,6,7) if that value Greater than count 8(means which is Sunday !)
For Sunday week day index is 1, day 8 is nothing but index 1.
Upvotes: 0
Reputation: 103
http://sqlfiddle.com/#!2/d41d8/50695
Maybe there will be no code on this link someday so I posted it here.
the upper link will show the number of the day and with date of the sundays. if you want to find any other day change the 1 at last to 2 for monday
select DATE_ADD('2012-12-15', INTERVAL ROW DAY) as Date,
row+1 as DayOfMonth from (
SELECT @row := @row + 1 as row FROM
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6) t1,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6) t2,
(SELECT @row:=-1) t3 limit 31
) b
where
DATE_ADD('2012-12-01', INTERVAL ROW DAY)
between '2012-12-01' and '2012-12-31'
and
DAYOFWEEK(DATE_ADD('2012-12-01', INTERVAL ROW DAY))=1
Upvotes: 1
Reputation: 33935
Does this work...
SELECT FLOOR(
(DATEDIFF(
'@enddate'
,'@startdate' + INTERVAL
LENGTH(SUBSTRING_INDEX('654321',WEEKDAY('@startdate'),1))
DAY) + 1)/7) + 1 x;
Upvotes: 0
Reputation: 18600
Try this
select count(DAYNAME(your_date_field)='Sunday') as sunday from tbl_name where your_date_field between 'date1' AND 'date2'
Upvotes: -1
Reputation: 741
Refer this, mysql have a function dayofweek, you can easily calculate starting week day and after that divide the number of days with 7:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_dayofweek
OR
SELECT COUNT(*) FROM table_name
WHERE date_column_of_table_name BETWEEN '2013-01-11' AND '2013-03-01'
AND WEEKDAY(date_column_of_table_name) = 6
Upvotes: -1
Reputation: 2729
SELECT COUNT(*) FROM table_name
WHERE column BETWEEN 'start_date' AND 'end_date'
AND WEEKDAY(date) = 6;
Upvotes: -1