EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

MySQL WEEKDAY

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » MySQL Tutorial » MySQL WEEKDAY

MySQL WEEKDAY

Introduction to MySQL WEEKDAY

WEEKDAY function in MySQL returns the day index corresponding to a date. This function accepts only one argument at a time in either a date format or date_time format. It is a valid case to use the CURDATE function to specify the date as an argument in WEEKDAY function. The function assigns 0 to Monday, 1 to Tuesday, 2 to Wednesday, … and 6 to Sunday. So all the seven days of a week are having a digit attached to it which will be returned as the output. The function returns a valid index for a valid date, but returns ‘NULL’ value for an invalid date, 0 value, and blank parameters.

Let’s see the syntax and examples of this function.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax

The syntax for the WEEKDAY function is as below:

WEEKDAY (date);

  • The parameter input is the calendar date of which index is to be retrieved.

We can see examples of the usage as below:

How FORMAT function works in MySQL?

The WEEKDAY function returns an INT output, a digit index of the week corresponding to the calendar date. We can see different possible examples as below.

Code:

SELECT WEEKDAY ('2020-01-01'), DAYNAME ('2020-01-01');

This query is to return the day index and day name of 01 January 2020.

Output:

INT output

So we can see that, the output for WEEKDAY function is 2 because it is the week day index corresponding to 01 January, 2020, which is a Wednesday.

Code:

Let’s write a query to return the weekday index of a date when the argument is input in date_time format.

SELECT WEEKDAY('2020-01-10 20:00:00');

Output:

date_time

The 10th of January 2020, is a Friday and thus the output of the query is 4.

Popular Course in this category
MySQL Training Program (11 Courses, 10 Projects)11 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (3,022 ratings)
Course Price

View Course

Related Courses
MS SQL Training (13 Courses, 11+ Projects)Oracle Training (14 Courses, 8+ Projects)PL SQL Training (4 Courses, 2+ Projects)

Code:

We saw valid scenarios in which we retrieve valid day index for the function. Let’s see an invalid date and corresponding output.

SELECT WEEKDAY('2020-02-30');

Output:

retrieve valid day

Month of February does not have day 30. So this is an invalid date. So the output is ‘NULL’.

Code:

If no argument is passed in the function, the expected output is ‘NULL’.

SELECT WEEKDAY('');

Output:

passed

Code:

Just like an invalid date, a 0 date is also expected to return NULL as the output.

SELECT WEEKDAY('0000-00-00');

Output:

MySQL WEEKDAY - 5

The date argument input ‘0000-00-00’ is an invalid date and thus it also returns NULL as output.

SCENARIO

We already discussed that the WEEKDAY function picks only one argument in a line of query. If multiple dates are to be input, then a separate WEEKDAY function call must be made. Executing WEEKDAY function with multiple date inputs within same parenthesis will lead to error output.

Code:

SELECT WEEKDAY('2020-02-03','202020-05-10');

Output:

MySQL WEEKDAY - 6

We have received an error message as the query is an error. The response is “Error Code: 1582. Incorrect parameter count in the call to native function ‘WEEKDAY’”. And the response says the parameter count in the call is incorrect.

Code:

Let’s write a query to retrieve all seven days of a week, in order.

SELECT WEEKDAY('2020-02-03') Feb_03,
WEEKDAY('2020-02-04') Feb_04,
WEEKDAY('2020-02-05') Feb_05,
WEEKDAY('2020-02-06') Feb_06,
WEEKDAY('2020-02-07') Feb_07,
WEEKDAY('2020-02-08') Feb_08,
WEEKDAY('2020-02-09') Feb_09;

We have used the WEEKDAY function seven times to get valid output.

Output:

MySQL WEEKDAY - 7

If we have a look at the calendar, we can identify that February 03, 2020, is a Monday and index value returned is 0, February 04 is Tuesday and index value returned is 1, February 05 is Wednesday and index value returned is 2, February 06 is Thursday and index value returned is 3, February 07 is Friday and index value returned is 4, February 08 is Saturday and index value returned is 5 and finally February 09 is a Sunday and index value returned is 6.

Code:

We can also try the CURDATE function within the WEEKDAY function to get the weekday index of the current date.

SELECT CURDATE() Today, DAYNAME (CURDATE()) Today_dayname, WEEKDAY(CURDATE()) Today_weekday;

Query calls CURDATE function to retrieve the current date, also the CURDATE function is passed as an input argument in DAYNAME function to retrieve the day name of the current date, and in WEEKDAY function to retrieve day index of the current date.

Output:

MySQL WEEKDAY - 8

If we study our output closely, we can understand, the current date, 19th June 2020 is returned as Today. Corresponding day name for 19th June is Friday and thus the weekday function returns the output as 4.

Conclusion – MySQL WEEKDAY

WEEKDAY function is used to retrieve the index of days in a week and it always outputs an INT value. The valid input formats are CURDATE() function, DATE format, and DATE_TIME format. The function allows only one argument per function call. Invalid date, zero date, and blank values return ‘NULL’ as output in this function. The function assigns an index of 0 to Monday, 1 to Tuesday, and thus 6 to Sunday.

Recommended Articles

This is a guide to MySQL WEEKDAY. Here we discuss an introduction to MySQL WEEKDAY, syntax how does it work with query examples. You can also go through our other related articles to learn more –

  1. MySQL DATE_SUB()
  2. MIN() in MySQL>
  3. MySQL avg()
  4. MySQL MAX() Function<

MySQL Training Program (11 Courses, 10 Projects)

11 Online Courses

10 Hands-on Projects

92+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL count()
    • MIN() in MySQL
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • TRUNCATE TABLE MySQL
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL 
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT 
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL COALESCE
    • MySQL Cluster
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • Introduction to MySQL
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • How To Install MySQL
    • MySQL OpenSource
    • MySQL Commands
    • Views in MySQL
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL UUID
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL Transaction
    • MySQL sort_buffer_size
    • MySQL Sync
    • MySQL Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - MySQL Training Program (11 Courses, 10 Projects) Learn More