How to use the MySQL DAYNAME() function
The DAYNAME() function in MySQL returns the name of the weekday for a given date. It can be useful for formatting dates and timestamps.
The DAYNAME() function in MySQL returns the name of the weekday for a given date. It can be useful for formatting dates and timestamps.
Syntax
The syntax for DAYNAME() is:
DAYNAME(date)
Where date is the date whose weekday name you want to return.
Examples
-
Get the weekday name for a date:
SELECT DAYNAME('2023-02-15');Result:
Wednesday -
Get the weekday from a timestamp column:
SELECT DAYNAME(created_at) FROM posts;This would return the weekday name from the
created_attimestamp column. -
Format a date using DAYNAME():
SELECT CONCAT(DAYNAME(birthday), ', ', DATE(birthday)) FROM users;Result:
Friday, 2023-05-12 -
Use DAYNAME() in a WHERE clause:
SELECT * FROM timesheets WHERE DAYNAME(work_date) = 'Monday';Returns timesheets for Monday only.
-
Get full weekday name:
SELECT DAYNAME('2023-01-10') AS FullWeekday;Result:
Tuesday
Other Similar Functions
WEEKDAY()- Weekday index from dateDAYOFWEEK()- Index of weekdayDATE_FORMAT()- Format datesDAY()- Get day of month
So DAYNAME() provides a simple way to get the weekday name from a date in MySQL.