Sunday, December 22, 2024

MySQL DAYNAME() Function

 The DAYNAME() function in MySQL is used to return the name of the day (e.g., 'Sunday', 'Monday', etc.) for a given date.

Syntax:

DAYNAME(date)

Parameters:

  • date: The date value you want the day name for. It must be in a valid date format.

Example Usage:

  1. Basic Example:

    SELECT DAYNAME('2024-12-25') AS DayName;
    

    Result:

    DayName
    -------
    Wednesday
    
  2. From a Table: Suppose you have a table events with a column event_date:

    SELECT event_date, DAYNAME(event_date) AS DayName
    FROM events;
    
  3. Current Date: You can use CURDATE() to get the current date:

    SELECT DAYNAME(CURDATE()) AS DayName;
    

Use Cases:

  • Useful for generating reports based on the day of the week.
  • Organizing or grouping data by day names in queries.

Let me know if you need more examples!

No comments:

Post a Comment