Languages
[Edit]
EN

MySQL - convert date to weekday name

0 points
Created by:
Veer-Ahmad
427

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

Quick solution:

SELECT DAYNAME(date_expression) AS 'alias_name';

Simple example

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

Query:

SELECT DAYNAME('2021-01-01 11:41:31') AS 'day_name';

Result:

MySQL - DAYNAME() Friday - result
MySQL - DAYNAME() Friday - result

Practical example

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

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

Note:

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

Example

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

Query:

SELECT
    `id`, `username`, `registration_time`, 
    DAYNAME(`registration_time`) AS 'day_name'
FROM `users`;

Result:

MySQL - convert date to weekday name - result
MySQL - convert date to weekday name - result

Database preparation

create_tables.sql file:

CREATE TABLE `users` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`username` VARCHAR(50) NOT NULL,
	`registration_time` DATETIME NOT NULL,
	PRIMARY KEY (`id`)
);

insert_data.sql file:

INSERT INTO `users`
	(`username`, `registration_time`)
VALUES
	('Tom', '2021-01-01 11:41:31'),
	('Chris','2021-01-02 11:42:45'),
	('Jack','2021-01-03 15:13:39'),
    ('Kim','2021-01-03 15:24:51'),
    ('Marco','2021-01-04 22:35:38'),
	('Kate','2021-01-04 22:46:51'),
	('Nam','2021-01-04 22:57:37');

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