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.

Posted on

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

  1. Get the weekday name for a date:

    SELECT DAYNAME('2023-02-15');
    

    Result: Wednesday

  2. Get the weekday from a timestamp column:

    SELECT DAYNAME(created_at) FROM posts;
    

    This would return the weekday name from the created_at timestamp column.

  3. Format a date using DAYNAME():

    SELECT CONCAT(DAYNAME(birthday), ', ', DATE(birthday))
    FROM users;
    

    Result: Friday, 2023-05-12

  4. Use DAYNAME() in a WHERE clause:

    SELECT * FROM timesheets
    WHERE DAYNAME(work_date) = 'Monday';
    

    Returns timesheets for Monday only.

  5. Get full weekday name:

    SELECT DAYNAME('2023-01-10') AS FullWeekday;
    

    Result: Tuesday

Other Similar Functions

  • WEEKDAY() - Weekday index from date
  • DAYOFWEEK() - Index of weekday
  • DATE_FORMAT() - Format dates
  • DAY() - Get day of month

So DAYNAME() provides a simple way to get the weekday name from a date in MySQL.