Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

MySQL Week Function

MySQL Week Date Functions, which returns the Week number of the given date. This MySQL Week function accepts two arguments. The first one is Date, and the second argument decides whether the Week starts with Sunday or Monday. And, the return value should be in range 0 to 53 or 1 to 53.

MySQL Week Syntax

The basic syntax of the Week Function in MySQL is as shown below:

Week(date, Mode);

Here, Mode is an optional argument. The following table shows you the Mode argument return values. For example, if Mode = 0, then the week starts on Sunday, and the return value is between 0 and 53.

ModeFirst Day of the WeekRangeWeek 1 is the First Week…
0Sunday0 – 53Sunday in the Given Year
1Monday 0 – 53with 4 or more days this Year
2Sunday1 – 53Sunday in the Given Year
3Monday1 – 53with 4 or more days this Year
4Sunday0 – 53with 4 or more days this Year
5Monday0 – 53Monday in the Given Year
6Sunday1 – 53with 4 or more days this Year
7Monday1 – 53Monday in the Given Year

MySQL Week function Example

The following examples help you understand the use of this MySQL week function. Here, we are returning the week number without Mode argument, and with mode argument.

SELECT WEEK('2019-02-21');

SELECT WEEK('2019-02-21', 0);

SELECT WEEK('2019-02-21', 1);
MySQL Week Function 1

Let us see another example of the Week Date Function.

SELECT WEEK('2018-12-31', 1);

SELECT WEEK('2018-12-31', 2);

SELECT WEEK('2018-12-31', 3);
MySQL Week Function 2

MySQL Week Example 2

Let us see another example of the Week function. This time, we are using a few more mode values and different MySQL dates.

SELECT WEEK('2018-10-31', 4);

SELECT WEEK('2018-05-24', 1);

SELECT WEEK('2018-02-02', 3);
MySQL Week Function 3

The Week function returns 0 if the date falls on the last week of the previous week. To avoid this, you can use 2, 3, 6, or 7 as a Mode argument value.

SELECT WEEK('2018-01-01', 0);

SELECT WEEK('2018-01-01', 2);

SELECT WEEK('2018-01-01', 6);
MySQL Week Function 4

Filed Under: MySQL

  • How to Download MySQL
  • Install MySQL on Windows
  • MySQL Create Database
  • MySQL Delete Database
  • MySQL Create Table
  • MySQL Drop Table
  • MySQL SELECT Statement
  • MySQL ALIAS Column
  • MySQL Distinct
  • MySQL Insert Statement
  • MySQL Delete
  • MySQL Truncate Table
  • MySQL WHERE Clause
  • MySQL Order By
  • MySQL Group By
  • MySQL Having Clause
  • MySQL LIMIT
  • MySQL Arithmetic Operators
  • MySQL COALESCE Function
  • MySQL AND Operator
  • MySQL NOT Operator
  • MySQL OR Operator
  • MySQL XOR Operator
  • MySQL BETWEEN Operator
  • MySQL Not Between Operator
  • MySQL GREATEST Function
  • MYSQL LEAST Function
  • MySQL LIKE Operator
  • MySQL NOT LIKE Operator
  • MySQL IFNULL Operator
  • MySQL NULLIF Operator
  • MySQL INTERVAL Operator
  • MySQL IS Operator
  • MySQL IN Operator
  • MySQL NOT IN Operator
  • MySQL IS NOT NULL
  • MySQL IS NULL
  • MySQL Inner Join
  • MySQL Cross Join
  • MySQL Right Join
  • MySQL Left Join
  • MySQL Aggregate Functions
  • MySQL Date Functions
  • MySQL Date Function
  • MySQL String Functions
  • MySQL Numeric Functions

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy