0
0
MysqlHow-ToBeginner · 3 min read

How to Use DAYNAME() Function in MySQL: Syntax and Examples

In MySQL, use the DAYNAME() function to get the name of the weekday from a date value. For example, DAYNAME('2024-06-01') returns 'Saturday'. This function helps convert dates into readable weekday names.
📐

Syntax

The DAYNAME() function takes one argument, a date or datetime value, and returns the full name of the weekday as a string.

  • date: A valid date or datetime expression.
sql
DAYNAME(date)
💻

Example

This example shows how to use DAYNAME() to get the weekday name from a date literal and from a table column.

sql
SELECT DAYNAME('2024-06-01') AS WeekdayName;

-- Example with a table
CREATE TEMPORARY TABLE events (event_date DATE);
INSERT INTO events VALUES ('2024-06-01'), ('2024-06-02');
SELECT event_date, DAYNAME(event_date) AS WeekdayName FROM events;
Output
WeekdayName Saturday event_date | WeekdayName 2024-06-01 | Saturday 2024-06-02 | Sunday
⚠️

Common Pitfalls

Common mistakes when using DAYNAME() include:

  • Passing invalid or NULL dates, which returns NULL.
  • Confusing DAYNAME() with DAYOFWEEK() which returns a number, not a name.
  • Using datetime values without proper formatting may cause unexpected results.

Always ensure the input is a valid date or datetime.

sql
/* Wrong: Passing a string not in date format */
SELECT DAYNAME('06-01-2024'); -- May return NULL or error depending on SQL mode

/* Right: Use proper date format YYYY-MM-DD */
SELECT DAYNAME('2024-06-01'); -- Returns 'Saturday'
📊

Quick Reference

FunctionDescriptionExampleReturns
DAYNAME(date)Returns weekday name from dateDAYNAME('2024-06-01')'Saturday'
DAYOFWEEK(date)Returns weekday number (1=Sunday)DAYOFWEEK('2024-06-01')7

Key Takeaways

Use DAYNAME() to get the full weekday name from a date in MySQL.
Input must be a valid date or datetime in 'YYYY-MM-DD' format for correct results.
DAYNAME() returns a string like 'Monday', 'Tuesday', etc.
Avoid confusing DAYNAME() with DAYOFWEEK(), which returns a number.
Passing invalid dates to DAYNAME() returns NULL.