{"id":2902,"date":"2018-07-06T15:25:46","date_gmt":"2018-07-06T22:25:46","guid":{"rendered":"https:\/\/database.guide\/?p=2902"},"modified":"2021-01-28T14:10:35","modified_gmt":"2021-01-28T04:10:35","slug":"weekday-examples-mysql","status":"publish","type":"post","link":"https:\/\/database.guide\/weekday-examples-mysql\/","title":{"rendered":"WEEKDAY() Examples &#8211; MySQL"},"content":{"rendered":"<p>In <a href=\"https:\/\/database.guide\/what-is-mysql\/\">MySQL<\/a>, you can use the <code>WEEKDAY()<\/code> function to return the weekday index for a given date. Simply provide the date as an argument and the function will return the weekday index.<\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>The syntax goes like this:<\/p>\n<pre>WEEKDAY(date)\r\n<\/pre>\n<p>Where <code>date<\/code> is the date you want the weekday returned from.<\/p>\n<h2>Example 1 &#8211; Basic Usage<\/h2>\n<p>Here&#8217;s an example to demonstrate.<\/p>\n<pre>SELECT WEEKDAY('2021-01-21') AS 'Weekday';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+---------+\r\n| Weekday |\r\n+---------+\r\n|       3 |\r\n+---------+\r\n<\/pre>\n<h2>Example 2 &#8211; Comparison with DAYNAME()<\/h2>\n<p>To make it easier for us to verify the results, here I run both the <code>WEEKDAY()<\/code> and <a href=\"https:\/\/database.guide\/dayname-examples-mysql\/\"><code>DAYNAME()<\/code><\/a> functions side by side.<\/p>\n<pre>SET @date = '2021-01-21';\r\nSELECT \r\n  DAYNAME(@date) AS 'Day Name',\r\n  WEEKDAY(@date) AS 'Weekday';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+----------+---------+\r\n| Day Name | Weekday |\r\n+----------+---------+\r\n| Thursday |       3 |\r\n+----------+---------+\r\n<\/pre>\n<h2>Weekday Index<\/h2>\n<p>If you&#8217;re wondering why Thursday is only the 3rd day of the week,\u00a0here&#8217;s how the weekday index is calculated:<\/p>\n<table>\n<thead>\n<tr>\n<th>Index<\/th>\n<th>Week Day<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>Monday<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>Tuesday<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Wednesday<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Thursday<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Friday<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>Saturday<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>Sunday<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, you can use the WEEKDAY() function to return the weekday index for a given date. Simply provide the date as an argument and the function will return the weekday index.<\/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-2902","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\/2902","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=2902"}],"version-history":[{"count":3,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2902\/revisions"}],"predecessor-version":[{"id":2905,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2902\/revisions\/2905"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}