MariaDB WEEKOFYEAR() Function

In MariaDB, WEEKOFYEAR() is a built-in function that returns the week number for a given date.

This function is equivalent to WEEK(date,3).

MariaDB WEEKOFYEAR() Syntax

This is the syntax of the MariaDB WEEKOFYEAR() function:

WEEKOFYEAR(date)

Parameters

date

Required. A date or datetime expression.

If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'WEEKOFYEAR'.

Return value

The MariaDB WEEKOFYEAR() function returns the week of the given date in the current year, and the value range is from 1 to 53.

The function assumes that “the first day of the week is Monday and the first week of the year has 3+ days”, which is equivalent to WEEK(date,3).

If the specified expression is not a valid date or datetime, the WEEKOFYEAR() function will return NULL.

If the argument is NULL, the WEEKOFYEAR() function will return NULL.

MariaDB WEEKOFYEAR() Examples

The following statement shows the basic usage of the MariaDB WEEKOFYEAR() function:

SELECT WEEKOFYEAR('2023-01-01');

Output:

+--------------------------+
| WEEKOFYEAR('2023-01-01') |
+--------------------------+
|                       52 |
+--------------------------+

The MariaDB WEEKOFYEAR() function allow you to use a datetime value:

SELECT WEEKOFYEAR('2023-01-01 10:10:10');

Output:

+-----------------------------------+
| WEEKOFYEAR('2023-01-01 10:10:10') |
+-----------------------------------+
|                                52 |
+-----------------------------------+

To get the week number for the current date, use the NOW() function:

SELECT
    NOW(),
    WEEKOFYEAR(NOW());

Output:

+---------------------+-------------------+
| NOW()               | WEEKOFYEAR(NOW()) |
+---------------------+-------------------+
| 2023-01-12 14:59:17 |                 2 |
+---------------------+-------------------+

Conclusion

In MariaDB, WEEKOFYEAR() is a built-in function that returns the week number for a given date.