How the DAYNAME() function works in Mariadb?
The DAYNAME()
function is a convenient function in Mariadb that returns the name of the weekday for a given date or datetime value.
The MariaDB DAYNAME()
function is used to retrieve the name of the weekday for a given date. This function is particularly useful for generating reports, scheduling applications, or any situation where the day of the week needs to be displayed or used in a query.
Syntax
The syntax for the MariaDB DAYNAME()
function is as follows:
DAYNAME(date)
date
is the date value from which you want to extract the weekday name.
Examples
Example 1: Getting the Day Name of a Specific Date
To find out the name of the weekday for a specific date:
SELECT DAYNAME('2024-03-17') AS weekday_name;
The output will be:
+--------------+
| weekday_name |
+--------------+
| Sunday |
+--------------+
Example 2: Day Name of the Current Date
To get the name of the weekday for the current date:
SELECT DAYNAME(CURDATE()) AS today_weekday;
The output will show the weekday name of today:
+---------------+
| today_weekday |
+---------------+
| Sunday |
+---------------+
Example 3: Filtering Records by Weekday Name
To filter records based on the weekday name:
DROP TABLE IF EXISTS events;
CREATE TABLE events (
id INT,
event_date DATE,
event_name VARCHAR(255)
);
INSERT INTO events VALUES (1, '2024-03-17', 'Concert'), (2, '2024-03-18', 'Conference');
SELECT * FROM events WHERE DAYNAME(event_date) = 'Sunday';
The output will display events that occur on a Sunday:
+------+------------+------------+
| id | event_date | event_name |
+------+------------+------------+
| 1 | 2024-03-17 | Concert |
+------+------------+------------+
Example 4: Grouping Records by Weekday Name
To group records by the name of the weekday:
SELECT DAYNAME(event_date) AS weekday, COUNT(*) AS total_events
FROM events
GROUP BY weekday;
The output will show the count of events grouped by the weekday name:
+---------+--------------+
| weekday | total_events |
+---------+--------------+
| Monday | 1 |
| Sunday | 1 |
+---------+--------------+
Example 5: Using DAYNAME()
in ORDER BY
To order records by the name of the weekday:
SELECT * FROM events ORDER BY DAYNAME(event_date);
The output will order the events by the weekday name:
+------+------------+------------+
| id | event_date | event_name |
+------+------------+------------+
| 2 | 2024-03-18 | Conference |
| 1 | 2024-03-17 | Concert |
+------+------------+------------+
Related Functions
Here are a few functions related to the MariaDB DAYNAME()
function:
- MariaDB
DAYOFWEEK()
function returns the index of the weekday (1 = Sunday, 2 = Monday, …, 7 = Saturday). - MariaDB
WEEKDAY()
function returns the index of the weekday (0 = Monday, 1 = Tuesday, …, 6 = Sunday). - MariaDB
DAYOFMONTH()
function returns the day of the month from a given date.
Conclusion
The DAYNAME()
function in MariaDB is a convenient way to work with the weekdays in dates. It allows for easy extraction of the weekday name, which can be used for display purposes or within logic in SQL queries. Whether you’re scheduling events, generating reports, or creating user-friendly interfaces, understanding how to use the DAYNAME()
function can greatly enhance your database’s capabilities.