Print

MySQL day of week ending on Friday

What?
Week Ending Date has been requested a lot more frequently now. It's an odd one but the example below shows how to do this for when the week ends on Friday. Assuming it starts on the previous Saturday.

Why?
Problems with MySQL weeks always starting on Sunday means this isn't very useful. I have another system which starts on Monday and ends on the following Sunday. The solution below is for the opposite, where the week starts on the previous Saturday and ends on the last working day of the week.

The Solution
Where "givenDate" is the given date you have to calculate the last working day of:
copyraw
-- SAMPLE DATES ARE: --
-- 2012-04-27 10:00:00 -- is a Friday
-- 2012-04-28 11:00:00 -- is a Saturday
-- 2012-04-29 12:00:00 -- is a Sunday


DATE_ADD(
	givenDate, 
	INTERVAL (
		6 - DATE_FORMAT(
			DATE_ADD(givenDate, INTERVAL 1 DAY), \'%w\'
		)
	) DAY
) AS WeDate


-- OR --

DATE_ADD(
	givenDate, 
	INTERVAL (
		7 - DAYOFWEEK(
			DATE_ADD(givenDate, INTERVAL 1 DAY)
		)
	) DAY
) AS WeDate


-- yields
-- 2012-04-27  -- this Friday
-- 2012-05-04  -- next Friday
-- 2012-05-04  -- next Friday
  1.  -- SAMPLE DATES ARE: -- 
  2.  -- 2012-04-27 10:00:00 -- is a Friday 
  3.  -- 2012-04-28 11:00:00 -- is a Saturday 
  4.  -- 2012-04-29 12:00:00 -- is a Sunday 
  5.   
  6.   
  7.  DATE_ADD( 
  8.      givenDate, 
  9.      INTERVAL ( 
  10.          6 - DATE_FORMAT( 
  11.              DATE_ADD(givenDate, INTERVAL 1 DAY), \'%w\' 
  12.          ) 
  13.      ) DAY 
  14.  ) AS WeDate 
  15.   
  16.   
  17.  -- OR -- 
  18.   
  19.  DATE_ADD( 
  20.      givenDate, 
  21.      INTERVAL ( 
  22.          7 - DAYOFWEEK( 
  23.              DATE_ADD(givenDate, INTERVAL 1 DAY) 
  24.          ) 
  25.      ) DAY 
  26.  ) AS WeDate 
  27.   
  28.   
  29.  -- yields 
  30.  -- 2012-04-27  -- this Friday 
  31.  -- 2012-05-04  -- next Friday 
  32.  -- 2012-05-04  -- next Friday 
Not sure why this works, thinking about it hurts my head but it worked with my test data (selecting current week days then the following saturday and sunday -- both saturday and sunday returned week ending date as next week).

If you were starting on Monday
...So if your week ends on a Sunday
copyraw
-- SAMPLE DATES ARE: --
-- 2012-04-27 10:00:00 -- is a Friday
-- 2012-04-28 11:00:00 -- is a Saturday
-- 2012-04-29 12:00:00 -- is a Sunday


DATE_ADD(
	givenDate, 
	INTERVAL (
		6 - weekday(givenDate)
	) DAY
) AS WeDate


-- yields
-- 2012-04-29  -- this Sunday
-- 2012-04-29  -- this Sunday
-- 2012-04-29  -- this Sunday
  1.  -- SAMPLE DATES ARE: -- 
  2.  -- 2012-04-27 10:00:00 -- is a Friday 
  3.  -- 2012-04-28 11:00:00 -- is a Saturday 
  4.  -- 2012-04-29 12:00:00 -- is a Sunday 
  5.   
  6.   
  7.  DATE_ADD( 
  8.      givenDate, 
  9.      INTERVAL ( 
  10.          6 - weekday(givenDate) 
  11.      ) DAY 
  12.  ) AS WeDate 
  13.   
  14.   
  15.  -- yields 
  16.  -- 2012-04-29  -- this Sunday 
  17.  -- 2012-04-29  -- this Sunday 
  18.  -- 2012-04-29  -- this Sunday 

Other Searches
Category: MySQL :: Article: 415