WEEKOFYEAR() Function in MySQL
Last Updated :
24 Apr, 2023
WEEKOFYEAR() function in MySQL is used to find the week number for a given date. If the date is NULL, the WEEKOFYEAR function will return NULL. Otherwise, it returns the value of week which ranges between 1 to 53.
Note: The WEEKOFYEAR() function considers the first week of the year to be the week that contains the first day of January. Also, the week number returned by WEEKOFYEAR() depends on the value of the `sql_mode` system variable. By default, WEEKOFYEAR() uses the mode that complies with ISO 8601.
Syntax:
WEEKOFYEAR( date)
Parameter : This method accepts only one parameter.
- date –The date or datetime from which we want to extract the week number.
Returns : It returns the week number. Example-1 : Finding the Current week number Using WEEKOFYEAR() Function on 29/09/2020.
SELECT WEEKOFYEAR(NOW()) AS Current_Week;
Output :
So, the current week number is 40. Example-2 : Finding the Week from given DateTime Using WEEKOFYEAR() Function.
SELECT WEEKOFYEAR('2018-04-22 08:09:22')
AS Week_Number ;
Output:
So, the week number is 16 in this example. Example-3 : Finding the Week from given date Using WEEKOFYEAR() Function.
SELECT WEEKOFYEAR('2019-07-25 ')
AS Week_Number ;
Output:
Example-4 : Finding the Week number from given datetime Using WEEKOFYEAR() Function when the date is NULL.
SELECT WEEKOFYEAR(NULL)
AS Week_Number;
Output:
Example-4: In this example, we are going to find the number of students enrolled in a course for every week in a year. 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-10-06' ),
( 'CS101', 161029, 'Arun Kumar', '2019-10-23' ),
( 'CS101', 161031, 'Sanya Jain', '2019-11-08' ),
( 'CS101', 161058, 'Riya Shah', '2019-11-20' ),
( 'CS101', 162051, 'Amit Sharma', '2019-11-30' ),
( 'CS101', 161951, 'Sayan Singh', '2019-12-07' ),
( 'CS101', 167051, 'Rishi Jana', '2019-12-15' ),
( 'CS101', 168001, 'Aniket Dravid', '2019-12-25' ),
( 'CS101', 168051, 'Rita Singh', '2019-12-28' ),
( 'CS101', 166051, 'Kalyan Ghandi', '2019-12-29' ) ;
So, Our table looks like.
Course_Name |
Student_id |
Student_name |
Enroll_Date |
CS101 |
161011 |
Amit Singh |
2019-10-06 |
CS101 |
161029 |
Arun Kumar |
2019-10-23 |
CS101 |
161031 |
Sanya Jain |
2019-11-08 |
CS101 |
161058 |
Riya Shah |
2019-11-20 |
CS101 |
162051 |
Amit Sharma |
2019-11-30 |
CS101 |
161951 |
Sayan Singh |
2019-12-07 |
CS101 |
167051 |
Rishi Jana |
2019-12-15 |
CS101 |
168001 |
Aniket Dravid |
2019-12-25 |
CS101 |
168051 |
Rita Singh |
2019-12-28 |
CS101 |
166051 |
Kalyan Ghandi |
2019-12-39 |
Now, we are going to find the number of students enrolled in the course every week.
SELECT
WEEKOFYEAR(Enroll_Date) Week_Number,
COUNT(Student_id) Student_Enrolled
FROM
Course
GROUP BY WEEKOFYEAR(Enroll_Date)
ORDER BY WEEKOFYEAR(Enroll_Date);
Output:
Week_Number |
Student_Enrolled |
40 |
1 |
43 |
1 |
45 |
1 |
47 |
1 |
48 |
1 |
49 |
1 |
50 |
1 |
52 |
3 |
Similar Reads
WEEK() Function in MySQL
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, W
3 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
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
REPEAT() function in MySQL
REPEAT() : This function in MySQL is used to repeat a string a specified number of times. Syntax : REPEAT(str, count) Parameters : This method accepts two parameter. str -Input String which we want to repeat. count -It will describe that how many times to repeat the string. Returns : It returns a re
2 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
POW() Function in MySQL
POW() function : This function in MySQL is used to return a results after raising a specified exponent number to a specified base number. For example if the base is 5 and exponent is 2, this will return a result of 25. Syntax : SELECT POW(x, y); Parameter : This method accepts two parameters as give
1 min read
SECOND() Function in MySQL
SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59. In this fu
2 min read
YEAR() Function in SQL Server
The YEAR() function in SQL Server is a powerful tool designed to extract the year component from a given date or datetime expression. It allows users to isolate the year as an integer value and facilitating various date-related operations and analyses. In this article, We will learn about the YEAR()
2 min read
TO DAYS() FUNCTION in MySQL
TO DAYS() : TO DAYS() function in MySQL takes the given date and returns a number of days since year 0 corresponding to the given date.TO DAYS() function can only be used with the dates within the Gregorian calendar. Syntax : TO DAYS(date) Parameters: The function can accept only one argument as sho
1 min read