Sunday, December 22, 2024

MySQL DAYOFMONTH() Function

 The DAYOFMONTH() function in MySQL is used to return the day of the month (from 1 to 31) for a given date.

Syntax:

DAYOFMONTH(date)

Parameters:

  • date: A valid date or datetime expression. If the input is not a valid date, the function returns NULL.

Example 1: Basic Usage

SELECT DAYOFMONTH('2024-12-23') AS DayOfMonth;

Output:

23

Example 2: Using Current Date

SELECT DAYOFMONTH(CURDATE()) AS TodayDayOfMonth;

Example 3: Invalid Date

SELECT DAYOFMONTH('2024-02-30') AS DayOfMonth;

Output:

NULL

Notes:

  • The DAYOFMONTH() function is equivalent to DAY().
  • It is commonly used in queries where day-level extraction is required for filtering, grouping, or calculations.

Related Functions:

  • DAY(): Returns the day of the month (same as DAYOFMONTH).
  • DAYOFWEEK(): Returns the weekday index (1 for Sunday, 7 for Saturday).
  • DAYOFYEAR(): Returns the day of the year (1 to 366).

No comments:

Post a Comment