Tuesday, December 24, 2024

MySQL WEEKOFYEAR() Function

 The WEEKOFYEAR() function in MySQL is used to return the week number for a given date. The week starts from Sunday by default, and it considers the first week of the year to be the week containing January 1st. The week number is a value from 1 to 53.

Syntax

WEEKOFYEAR(date)

Parameters

  • date: The date for which you want to calculate the week of the year.

Example Usage

SELECT WEEKOFYEAR('2024-01-01') AS WeekNumber; -- Returns 1
SELECT WEEKOFYEAR('2024-12-31') AS WeekNumber; -- Returns 53

Notes

  1. Related Functions:
    • WEEK(): Offers more options for specifying week mode (start of week, year handling).
    • YEARWEEK(): Combines year and week into a single value.
  2. The WEEKOFYEAR() function is equivalent to using WEEK(date, 3).

Let me know if you'd like more examples or explanations!

No comments:

Post a Comment