Languages
[Edit]
EN

MySQL - convert date to weekday number

0 points
Created by:
Emelia
517

In this article, we would like to show you how to convert date to weekday number in MySQL.

Quick solution:

or 

Note:

Go to this article to read about differences between DAYOFWEEK() and WEEKDAY() functions.

Simple example

Edit

In this example, we will convert example date ( 2021-01-01 11:41:31 - Friday) to weekday number.

DAYOFWEEK() example

Edit

Query:

Result:

MySQL - DAYOFWEEK() Friday - result
MySQL - DAYOFWEEK() Friday - result

WEEKDAY() example

Edit

Query:

Result:

MySQL - WEEKDAY() Friday - result
MySQL - WEEKDAY() Friday - result

Practical example

Edit

To show how to convert date to weekday number, we will use the following table:

MySQL - example data used to convert date to weekday number
MySQL - example data used to convert date to weekday number

Note:

At the end of this article you can find database preparation SQL queries.

Example 1 - DAYOFWEEK() function

Edit

In this example, we will convert registration_time column from users table to the day week number using DAYOFWEEK() function.

Query:

Result:

MySQL - convert date to weekday number - result
MySQL - convert date to weekday number - result

Example 2 - WEEKDAY() function

Edit

In this example, we will convert registration_time column from users table to the day week number using WEEKDAY() function.

Query:

Result:

MySQL - convert date to weekday number - result
MySQL - convert date to weekday number - result

Database preparation

Edit

create_tables.sql file:

insert_data.sql file:

References

Edit
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL - dates

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join