WEEK() function in MySQL is a versatile built-in date function designed to extract the week number from a given date. This function is particularly beneficial for grouping and analyzing data based on weekly intervals, allowing for more insightful data interpretation and reporting.
In this article, We will learn about the WEEK() Function in MySQL in detail by understanding various examples and so on.
WEEK() Function in MySQL
- The
WEEK()
function in MySQL is a built-in date function that returns the week number of a given date.
- It can be particularly useful for grouping or analyzing data based on weeks.
Syntax:
WEEK(date[, mode])
Parameters:
- date: The date value from which to extract the week number. This can be a date or datetime expression.
- mode (optional): An integer that specifies the mode for determining the first week of the year and the first day of the week. If not specified, the default mode is 0.
MODE | FIRST DAY OF WEEK | RANGE | WEEK 1 IS THE FIRST WEEK … |
---|
0 | Sunday | 0-53 | with a Sunday in this year |
1 | Monday | 0-53 | with 4 or more days this year |
2 | Sunday | 1-53 | with a Sunday in this year |
3 | Monday | 1-53 | with 4 or more days this year |
4 | Sunday | 0-53 | with 4 or more days this year |
5 | Monday | 0-53 | with a Monday in this year |
6 | Sunday | 1-53 | with 4 or more days this year |
7 | Monday | 1-53 | with a Monday in this year |
Examples of WEEK() Function in MySQL
Example 1:
Finding the Current week number Using WEEK() Function on 15/10/2020.
SELECT WEEK(NOW()) AS Current_Week;
Output:
So, the current week number is 41.
Example 2:
Finding the Week from given datetime Using WEEK() Function.
SELECT WEEK('2010-05-20 08:09:22') AS Week;
Output :
So, week number is 20 in this example.
Example 3:
Finding the Week from given datetime Using WEEK() Function when the date is NULL.
SELECT WEEK(NULL) AS Week;
Output :
Example 4:
In this example we are going to find number of student enrolled in a course for every week. To demonstrate create a table named Course.
CREATE TABLE Course(
Course_name VARCHAR(100) NOT NULL,
Student_id INT NOT NULL,
Student_name VARCHAR(100) NOT NULL,
Enroll_Date Date NOT NULL,
PRIMARY KEY(Student_id)
);
Now inserting some data to the Course table -
INSERT INTO
Course(Course_Name, Student_id, Student_name, Enroll_Date)
VALUES
('CS101', 161011, 'Amit Singh', '2019-1-26'),
('CS101', 161029, 'Arun Kumar', '2019-5-30'),
('CS101', 161031, 'Sanya Jain', '2019-6-08'),
('CS101', 161058, 'Riya Shah', '2019-10-15'),
('CS101', 162051, 'Amit Sharma', '2019-10-18'),
('CS101', 161951, 'Sayan Singh', '2019-10-30'),
('CS101', 167051, 'Rishi Jana', '2019-11-02'),
('CS101', 168001, 'Aniket Dravid', '2019-11-10'),
('CS101', 168051, 'Rita Singh', '2019-11-13'),
('CS101', 166051, 'Kalyan Ghandi', '2019-12-26');
Table: Course
COURSE_NAME | STUDENT_ID | STUDENT_NAME | ENROLL_DATE |
---|
CS101 | 161011 | Amit Singh | 2019-1-26 |
CS101 | 161029 | Arun Kumar | 2019-5-30 |
CS101 | 161031 | Sanya Jain | 2019-6-08 |
CS101 | 161058 | Riya Shah | 2019-10-15 |
CS101 | 162051 | Amit Sharma | 2019-10-18 |
CS101 | 161951 | Sayan Singh | 2019-10-30 |
CS101 | 167051 | Rishi Jana | 2019-11-02 |
CS101 | 168001 | Aniket Dravid | 2019-11-10 |
CS101 | 168051 | Rita Singh | 2019-11-13 |
CS101 | 166051 | Kalyan Ghandi | 2019-12-26 |
Now, we are going to find number of student enrolled in the course for every week.
SELECT WEEK(Enroll_Date) WeekNumber,
COUNT(Student_id) Student_Enrolled
FROM Course
GROUP BY WEEK(Enroll_Date)
ORDER BY WEEK(Enroll_Date);
Output:
WEEKNUMBER | STUDENT_ENROLLED |
---|
3 | 1 |
21 | 1 |
22 | 1 |
41 | 2 |
43 | 2 |
45 | 2 |
51 | 1 |
Conclusion
In summary, the WEEK() function serves as a powerful tool for deriving week numbers from dates in MySQL. Its ability to accommodate different modes for defining the start of the week and the first week of the year adds flexibility for users.
Similar Reads
WEEKDAY() Function in MySQL
WEEKDAY() function in MySQL is used to find the weekday value for a given date. If the date is NULL, the WEEKDAY() function will return NULL. Otherwise, it returns index for a date i.e., 0 for Monday, 1 for Tuesday, ⦠6 for Sunday. Syntax : WEEKDAY(date) Parameter : This method accepts one parameter
3 min read
YEARWEEK() Function in MySQL
YEARWEEK() function in MySQL is used to find year and week for a given date. If the date is NULL, the YEARWEEK() function will return NULL. Otherwise, it returns value of year which range from 1000 to 9999 and value of week which ranges between 0 to 53. Syntax : YEARWEEK(date, mode) Parameter : This
4 min read
POWER() Function in MySQL
POWER() function in MySQL is used to find the value of a number raised to the power of another number. It Returns the value of X raised to the power of Y. Syntax : POWER(X, Y) Parameter : This method accepts two parameter which are described below : X : It specifies the base number. Y : It specifies
3 min read
TIME() Function in MySQL
The TIME() function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'. This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn ab
4 min read
YEAR() Function in MySQL
YEAR() function in MySQL is used to find year from the given date. If the date is NULL, the YEAR() function will return NULL. Otherwise, it returns value range from 1000 to 9999. Syntax : YEAR(date) Parameter : This method accepts one parameter as mentioned above and described below : date : The dat
3 min read
PI() function in MySQL
PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally. Syntax : PI() Parameter : This method does not accept any parameter. Returns : It returns the Pi value i.e. 3.141593. Example-1 :
2 min read
REPLACE() Function in MySQL
The REPLACE() function in MySQL is a powerful tool for string manipulation, allowing users to substitute specific substrings within a larger string. This functionality is particularly useful in various applications such as updating text data, cleaning up input or adjusting content in a database. In
3 min read
MySQL MIN() Function
The MySQL MIN() function is used to get the smallest value in a number set. Suppose you have a table with a list of different products and their corresponding prices; you would want to know which one has the lowest price. Here, the MIN() function will return that answer to you in the easiest possibl
3 min read
SUM() Function in MySQL
The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read