Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting first day of the week in MySql using Week No

Tags:

sql

mysql

How do I get the first day of a given week whose week number is available?

For example as I write this post we are at WEEK 29.I would like to write a MySQL query that will return Sunday 18 July using this WEEKNO 29 as the only available parameter.

like image 292
davykiash Avatar asked Jul 23 '10 12:07

davykiash


People also ask

How do I get current week records in MySQL?

WEEK() function in MySQL is used to find week number for a given date. If the date is NULL, the WEEK() function will return NULL. Otherwise, it returns the value of week which ranges between 0 to 53. The date or datetime from which we want to extract the week.

Is there a week function in SQL?

MySQL WEEK() Function The WEEK() function returns the week number for a given date (a number from 0 to 53).

What is Date_add in MySQL?

The DATE_ADD() function adds a time/date interval to a date and then returns the date.


1 Answers

This is an accurate way of getting the first day of the week and the last day of the week based on the current date:

adddate(curdate(), INTERVAL 1-DAYOFWEEK(curdate()) DAY) WeekStart, adddate(curdate(), INTERVAL 7-DAYOFWEEK(curdate()) DAY) WeekEnd 
like image 101
David Merrill Avatar answered Sep 18 '22 16:09

David Merrill