{"id":2906,"date":"2018-07-06T17:03:45","date_gmt":"2018-07-07T00:03:45","guid":{"rendered":"https:\/\/database.guide\/?p=2906"},"modified":"2021-01-28T14:10:35","modified_gmt":"2021-01-28T04:10:35","slug":"weekofyear-examples-mysql","status":"publish","type":"post","link":"https:\/\/database.guide\/weekofyear-examples-mysql\/","title":{"rendered":"WEEKOFYEAR() Examples &#8211; MySQL"},"content":{"rendered":"<p>In <a href=\"https:\/\/database.guide\/what-is-mysql\/\">MySQL<\/a>, you can use the <code>WEEKOFYEAR()<\/code> function to return the calendar week for a given date. Simply provide the date as an argument and the function will return the calendar week as a number in the range from 1 to 53.<\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>The syntax goes like this:<\/p>\n<pre>WEEKOFYEAR(date)\r\n<\/pre>\n<p>Where <code>date<\/code> is the date you want the calendar week returned from.<\/p>\n<h2>Example 1 &#8211; Basic Usage<\/h2>\n<p>Here&#8217;s an example to demonstrate.<\/p>\n<pre>SELECT WEEKOFYEAR('2021-01-21') AS 'Week of the year';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+------------------+\r\n| Week of the year |\r\n+------------------+\r\n|                3 |\r\n+------------------+\r\n<\/pre>\n<p>Here&#8217;s an example using a later date:<\/p>\n<pre>SELECT WEEKOFYEAR('2021-12-21') AS 'Week of the year';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+------------------+\r\n| Week of the year |\r\n+------------------+\r\n|               51 |\r\n+------------------+\r\n<\/pre>\n<h2>Example 2 &#8211; WEEKOFYEAR() vs WEEK()<\/h2>\n<p>The <code>WEEKOFYEAR()<\/code> function is the equivalent of using <code>WEEK(date,3)<\/code>. In this context, <code>3<\/code> is the mode that determines whether the week starts on Sunday or Monday, and whether its count ranges from 0 to 53 or from 1 to 53.<\/p>\n<p>Here&#8217;s an example that shows these two functions side by side:<\/p>\n<pre>SET @date = '2021-07-21';\r\nSELECT \r\n  WEEK(@date, 3),\r\n  WEEKOFYEAR(@date);\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+----------------+-------------------+\r\n| WEEK(@date, 3) | WEEKOFYEAR(@date) |\r\n+----------------+-------------------+\r\n|             29 |                29 |\r\n+----------------+-------------------+\r\n<\/pre>\n<p>To see how the mode affects the result, see <a href=\"https:\/\/database.guide\/week-examples-mysql\/\"><code>WEEK()<\/code> Examples &#8211; MySQL<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, you can use the WEEKOFYEAR() function to return the calendar week for a given date. Simply provide the date as an argument and the function will return the calendar week as a number in the range from 1 to 53.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[68,115,93],"class_list":["post-2906","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-date-functions","tag-dates","tag-functions"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=2906"}],"version-history":[{"count":1,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2906\/revisions"}],"predecessor-version":[{"id":2907,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2906\/revisions\/2907"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}