©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The YEARWEEK SQL Function in MySQL and MariaDB - Year and week of date

YEARWEEKSyntax:YEARWEEK(Date [, Mode])
Return value:INTEGER
Function type:Date and Time function
Function Description

The YEARWEEK() function returns the year and week of "Date".

The "Date" argument must be of type DATE or DATETIME.

The "Mode" argument can be used to specify whether the week of the year starts on Sunday or Monday and whether the return value should be in the range 0 to 53 or 1 to 53.

If the "Mode" argument is omitted, the value of the default_week_format system variable is used.

If the value for "Date" is invalid or NULL, the YEARWEEK() function returns NULL.


Valid specifications for mode are:

Mode  1st day of week  Range  Week 1 is the 1st week with  
0Sunday0-53a Sunday in this year
1Monday0-53more than 3 days this year
2Sunday1-53a Sunday in this year
3Monday1-53more than 3 days this year
4Sunday0-53more than 3 days this year
5Monday0-53a Monday in this year
6Sunday1-53more than 3 days this year
7Monday1-53a Monday in this year

SQL Examples for the YEARWEEK function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

SELECT yearweek(now());
SELECT yearweek(adddate(now(), interval 1 day));
SELECT yearweek(utc_timestamp());
SELECT yearweek('2023-03-29');
SELECT yearweek('2023-03-29 12:15:30');
SELECT yearweek(20230216);
SELECT yearweek(20230216121530);

SELECT yearweek('2022-01-02', 0);
SELECT yearweek('2022-01-02', 1);
SELECT yearweek('2022-01-02', 2);
SELECT yearweek('2022-01-02', 3);
SELECT yearweek('2022-01-02', 4);
SELECT yearweek('2022-01-02', 5);
SELECT yearweek('2022-01-02', 6);
SELECT yearweek('2022-01-02', 7);

SELECT yearweek('2023-00-00');
SELECT yearweek(unix_timestamp());
SELECT yearweek('xxx');
SELECT yearweek(null);

SHOW VARIABLES LIKE 'default_week_format';

yearweek(now())
int(6)
202314
yearweek(adddate(now(), interval 1 day))
int(6)
202314
yearweek(utc_timestamp())
int(6)
202314
yearweek('2023-03-29')
int(6)
202313
yearweek('2023-03-29 12:15:30')
int(6)
202313
yearweek(20230216)
int(6)
202307
yearweek(20230216121530)
int(6)
202307
yearweek('2022-01-02', 0)
int(6)
202201
yearweek('2022-01-02', 1)
int(6)
202152
yearweek('2022-01-02', 2)
int(6)
202201
yearweek('2022-01-02', 3)
int(6)
202152
yearweek('2022-01-02', 4)
int(6)
202201
yearweek('2022-01-02', 5)
int(6)
202152
yearweek('2022-01-02', 6)
int(6)
202201
yearweek('2022-01-02', 7)
int(6)
202152
yearweek('2023-00-00')
int(6)
NULL
yearweek(unix_timestamp())
int(6)
NULL
yearweek('xxx')
int(6)
NULL
yearweek(null)
int(6)
NULL
Variable_name
Value
information_schema.SESSION_VARIABLES
VARIABLE_NAME
varchar(64) UTF8
information_schema.SESSION_VARIABLES
VARIABLE_VALUE
varchar(2048) UTF8
default_week_format0
The examples were created with the MyWAY SQL manager: Download

How to use the YEARWEEK() function in MySQL and MariaDB databases

In MySQL and MariaDB the YEARWEEK() function can be used to get the year and week number for a specific "Date" or "Date and Time" expression. The function returns an integer value containing the year and week combined in the format "YYYYWW", where in the week portion the smallest value is the week containing the first day of the year and the largest value is the week containing the last day of the year. As an optional parameter, the mode can be specified, which determines how the weekly portion is calculated. This result may vary depending on the specified mode. The default mode is 0.

Table column names or other expressions can be used as parameters to get the combination of year and week number in SQL queries, work with specific year-week combinations, or perform various operations based on the year-week combination, for example to sort, sum or group query results by those values.
VERSIONWEEKWEEKDAYWEEKOFYEARWEIGHT_STRINGYEARYEARWEEK

Further MySQL und MariaDB SQL Date and Time functions

ADDDATEADDDATE(Date, [INTERVAL] Number [Unit])
More about ADDDATE Function

ADDTIMEADDTIME(Date, Expression)
More about ADDTIME Function

CONVERT_TZCONVERT_TZ(Date, From time zone, To time zone)
More about CONVERT_TZ Function

CURDATECURDATE()
More about CURDATE Function

CURTIMECURTIME([Precision])
More about CURTIME Function

CURRENT_DATECURRENT_DATE(-)
More about CURRENT_DATE Function

CURRENT_TIMECURRENT_TIME([Precision])
More about CURRENT_TIME Function

CURRENT_TIMESTAMPCURRENT_TIMESTAMP([Precision])
More about CURRENT_TIMESTAMP Function

DATEDATE(Date and Time)
More about DATE Function

DATE_ADDDATE_ADD(Date, INTERVAL Number Unit)
More about DATE_ADD Function

DATE_SUBDATE_SUB(Date, INTERVAL Number Unit)
More about DATE_SUB Function

FROM_DAYSFROM_DAYS(Number of days)
More about FROM_DAYS Function

FROM_UNIXTIMEFROM_UNIXTIME(Unix timestamp [, Format])
More about FROM_UNIXTIME Function

LOCALTIMELOCALTIME([Precision])
More about LOCALTIME Function

LOCALTIMESTAMPLOCALTIMESTAMP([Precision])
More about LOCALTIMESTAMP Function

MAKEDATEMAKEDATE(Year, Day of year)
More about MAKEDATE Function

MAKETIMEMAKETIME(Hours, Minutes, Seconds)
More about MAKETIME Function

NOWNOW([Precision])
More about NOW Function

SEC_TO_TIMESEC_TO_TIME(Seconds)
More about SEC_TO_TIME Function

SLEEPSLEEP(Seconds)
More about SLEEP Function

STR_TO_DATESTR_TO_DATE(String, Format)
More about STR_TO_DATE Function

SUBDATESUBDATE(Date, [INTERVAL] Number [Unit])
More about SUBDATE Function

SUBTIMESUBTIME(Date and Time, Deduction)
More about SUBTIME Function

SYSDATESYSDATE()
More about SYSDATE Function

TIMETIME(Date and Time)
More about TIME Function

TIMEDIFFTIMEDIFF(Date 1, Date 2)
More about TIMEDIFF Function

TIMESTAMPTIMESTAMP(Date [, Addition])
More about TIMESTAMP Function

TIMESTAMPADDTIMESTAMPADD(Unit, Number, Date)
More about TIMESTAMPADD Function

TIMESTAMPDIFFTIMESTAMPDIFF(Unit, Date 1, Date 2)
More about TIMESTAMPDIFF Function

UTC_DATEUTC_DATE()
More about UTC_DATE Function

UTC_TIMEUTC_TIME([Precision])
More about UTC_TIME Function

UTC_TIMESTAMPUTC_TIMESTAMP([Precision])
More about UTC_TIMESTAMP Function

DAYDAY(Date)
More about DAY Function

DAYOFMONTHDAYOFMONTH(Date)
More about DAYOFMONTH Function

DAYOFWEEKDAYOFWEEK(Date)
More about DAYOFWEEK Function

DAYOFYEARDAYOFYEAR(Date)
More about DAYOFYEAR Function

EXTRACTEXTRACT(Unit FROM Date)
More about EXTRACT Function

HOURHOUR(Date and Time)
More about HOUR Function

MICROSECONDMICROSECOND(Date and Time)
More about MICROSECOND Function

MINUTEMINUTE(Date and Time)
More about MINUTE Function

MONTHMONTH(Date)
More about MONTH Function

QUARTERQUARTER(Date)
More about QUARTER Function

SECONDSECOND(Date and Time)
More about SECOND Function

TIME_TO_SECTIME_TO_SEC(Time)
More about TIME_TO_SEC Function

TO_DAYSTO_DAYS(Date)
More about TO_DAYS Function

TO_SECONDSTO_SECONDS(Date and Time)
More about TO_SECONDS Function

UNIX_TIMESTAMPUNIX_TIMESTAMP([Date and Time])
More about UNIX_TIMESTAMP Function

WEEKWEEK(Date [, Mode])
More about WEEK Function

WEEKDAYWEEKDAY(Date)
More about WEEKDAY Function

WEEKOFYEARWEEKOFYEAR(Date)
More about WEEKOFYEAR Function

YEARYEAR(Date)
More about YEAR Function

DAYNAMEDAYNAME(Date)
More about DAYNAME Function

DATE_FORMATDATE_FORMAT(Date and Time, Format)
More about DATE_FORMAT Function

GET_FORMATGET_FORMAT(Type, Format)
More about GET_FORMAT Function

MONTHNAMEMONTHNAME(Date)
More about MONTHNAME Function

TIME_FORMATTIME_FORMAT(Time, Format)
More about TIME_FORMAT Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the YEARWEEK SQL function: mysql.com and mariadb.com and w3schools.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer