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
    These didn't get me very far until I decided to write this article:
  • DAYOFWEEK(myDate, 1) or WEEKDAY(mydate, 1)
  • MySQL code for first and last day of week.
  • Day of Week starting on Monday.
Category: MySQL :: Article: 415

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Joes Word Cloud

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.