search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

MySQL | YEARWEEK method

schedule Aug 11, 2023
Last updated
local_offer
MySQL
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

MySQL's YEARWEEK(~) method returns the year and week for a date or datetime.

NOTE

The year in the returned value may differ from the year in the input date for first and last weeks of the year.

For example, if 1 Jan 2020 does not meet the definition of first week for 2020 as defined by the specified mode, it will be considered the last week of 2019.

Parameters

1. date | date/datetime

The date/datetime to return the year and week number for.

2. mode | number | optional

The convention for how a week should be defined. Defaults to Mode 0.

Mode

First day of week

Range

Week 1 is the first week ...

0

Sunday

0-53

with a Sunday

1

Monday

0-53

with 4 or more days

2

Sunday

1-53

with a Sunday

3

Monday

1-53

with 4 or more days

4

Sunday

0-53

with 4 or more days

5

Monday

0-53

with a Monday

6

Sunday

1-53

with 4 or more days

7

Monday

1-53

with a Monday

Return value

The year and week for the input date or datetime.

Examples

Basic usage

To return the year and week for 2020-01-01 as per Mode 0 definition:

SELECT YEARWEEK('2020-01-01');
+------------------------+
| YEARWEEK('2020-01-01') |
+------------------------+
| 201952 |
+------------------------+

The first Sunday in 2020 is on 5th Jan. Therefore 1st - 4th Jan 2020 is still considered part of 52nd week of 2019.

Mode parameter

To return the year and week for 2020-01-01 as per Mode 1 definition:

SELECT YEARWEEK('2020-01-01', 1);
+---------------------------+
| YEARWEEK('2020-01-01', 1) |
+---------------------------+
| 202001 |
+---------------------------+

As the week containing 1 Jan 2020 has 4 or more days, it is considered Week 1 of 2020.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!