{"id":2626,"date":"2018-06-20T21:33:42","date_gmt":"2018-06-21T04:33:42","guid":{"rendered":"https:\/\/database.guide\/?p=2626"},"modified":"2021-01-28T14:11:23","modified_gmt":"2021-01-28T04:11:23","slug":"subdate-examples-mysql","status":"publish","type":"post","link":"https:\/\/database.guide\/subdate-examples-mysql\/","title":{"rendered":"SUBDATE() Examples &#8211; MySQL"},"content":{"rendered":"<p>In <a href=\"https:\/\/database.guide\/what-is-mysql\/\">MySQL<\/a>, you can use the <code>SUBDATE()<\/code> function to subtract a specified amount of time from a date. For example, you could use it to subtract 10 days from a given date. You can specify whether to subtract days, weeks, months, quarters, years, etc. You can also subtract a time value, such as seconds, microseconds, etc.<\/p>\n<p>When using the first syntax listed below, the <code>SUBDATE()<\/code> function is a synonym for the <a href=\"https:\/\/database.guide\/date_sub-examples-mysql\/\"><code>DATE_SUB()<\/code><\/a> function (similar to how <a href=\"https:\/\/database.guide\/adddate-examples-mysql\/\"><code>ADDDATE()<\/code><\/a> is a synonym for <a href=\"https:\/\/database.guide\/date_add-examples-mysql\/\"><code>DATE_ADD()<\/code><\/a> when using the same syntax).<\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>You can use this function in the following two ways:<\/p>\n<pre>SUBDATE(date,INTERVAL expr unit)<\/pre>\n<p>Or<\/p>\n<pre>SUBDATE(expr,days)<\/pre>\n<h2>Example 1 &#8211; The First Syntax<\/h2>\n<p>Here&#8217;s an example of using the first form of the syntax.<\/p>\n<pre>SELECT SUBDATE('2018-05-10', INTERVAL 2 DAY) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+------------+\r\n| Result     |\r\n+------------+\r\n| 2018-05-08 |\r\n+------------+<\/pre>\n<p>This example subtracts 2 days from the date supplied by the first argument.<\/p>\n<h2>Example 2 &#8211; The Second Syntax<\/h2>\n<p>This example could be rewritten as the following:<\/p>\n<pre>SELECT SUBDATE('2018-05-10', 2) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+------------+\r\n| Result     |\r\n+------------+\r\n| 2018-05-08 |\r\n+------------+<\/pre>\n<p>This uses the second form of the syntax. The second argument is an integer that represents how many days should be subtracted from the date supplied by the first argument.<\/p>\n<p>As mentioned,\u00a0<code>SUBDATE()<\/code> is a synonym for <code>DATE_SUB()<\/code>, but\u00a0only when the first syntax is being used. The second syntax is only available in\u00a0<code>SUBDATE()<\/code>.<\/p>\n<h2>Example 3 &#8211; Other Date Units<\/h2>\n<p>One benefit of the first form of the syntax is that you can specify whether to subtract days, weeks, months, years, etc. Here are some examples.<\/p>\n<pre>SELECT \r\n    '2018-05-10' AS 'Start Date',\r\n    SUBDATE('2018-05-10', INTERVAL 2 WEEK) AS '-2 Weeks',\r\n    SUBDATE('2018-05-10', INTERVAL 2 MONTH) AS '-2 Months',\r\n    SUBDATE('2018-05-10', INTERVAL 2 QUARTER) AS '-2 Quarters',\r\n    SUBDATE('2018-05-10', INTERVAL 2 YEAR) AS '-2 Years';<\/pre>\n<p>Result:<\/p>\n<pre>+------------+------------+------------+-------------+------------+\r\n| Start Date | -2 Weeks   | -2 Months  | -2 Quarters | -2 Years   |\r\n+------------+------------+------------+-------------+------------+\r\n| 2018-05-10 | 2018-04-26 | 2018-03-10 | 2017-11-10  | 2016-05-10 |\r\n+------------+------------+------------+-------------+------------+<\/pre>\n<h2>Example 4 &#8211; Time Units<\/h2>\n<p>You can also use <code>SUBDATE()<\/code> to subtract time units from a date\/time value. Here&#8217;s an example.<\/p>\n<pre>SELECT SUBDATE('2018-05-10 01:00:00', INTERVAL 2 HOUR) AS Result;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+---------------------+\r\n| Result              |\r\n+---------------------+\r\n| 2018-05-09 23:00:00 |\r\n+---------------------+<\/pre>\n<h2>Expected Values<\/h2>\n<p>The following table shows the valid unit values and their expected format.<\/p>\n<table>\n<thead>\n<tr>\n<th><code>unit<\/code> Value<\/th>\n<th>Expected <code>expr<\/code> Format<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>MICROSECOND<\/td>\n<td>MICROSECONDS<\/td>\n<\/tr>\n<tr>\n<td>SECOND<\/td>\n<td>SECONDS<\/td>\n<\/tr>\n<tr>\n<td>MINUTE<\/td>\n<td>MINUTES<\/td>\n<\/tr>\n<tr>\n<td>HOUR<\/td>\n<td>HOURS<\/td>\n<\/tr>\n<tr>\n<td>DAY<\/td>\n<td>DAYS<\/td>\n<\/tr>\n<tr>\n<td>WEEK<\/td>\n<td>WEEKS<\/td>\n<\/tr>\n<tr>\n<td>MONTH<\/td>\n<td>MONTHS<\/td>\n<\/tr>\n<tr>\n<td>QUARTER<\/td>\n<td>QUARTERS<\/td>\n<\/tr>\n<tr>\n<td>YEAR<\/td>\n<td>YEARS<\/td>\n<\/tr>\n<tr>\n<td>SECOND_MICROSECOND<\/td>\n<td>&#8216;SECONDS.MICROSECONDS&#8217;<\/td>\n<\/tr>\n<tr>\n<td>MINUTE_MICROSECOND<\/td>\n<td>&#8216;MINUTES:SECONDS.MICROSECONDS&#8217;<\/td>\n<\/tr>\n<tr>\n<td>MINUTE_SECOND<\/td>\n<td>&#8216;MINUTES:SECONDS&#8217;<\/td>\n<\/tr>\n<tr>\n<td>HOUR_MICROSECOND<\/td>\n<td>&#8216;HOURS:MINUTES:SECONDS.MICROSECONDS&#8217;<\/td>\n<\/tr>\n<tr>\n<td>HOUR_SECOND<\/td>\n<td>&#8216;HOURS:MINUTES:SECONDS&#8217;<\/td>\n<\/tr>\n<tr>\n<td>HOUR_MINUTE<\/td>\n<td>&#8216;HOURS:MINUTES&#8217;<\/td>\n<\/tr>\n<tr>\n<td>DAY_MICROSECOND<\/td>\n<td>&#8216;DAYS HOURS:MINUTES:SECONDS.MICROSECONDS&#8217;<\/td>\n<\/tr>\n<tr>\n<td>DAY_SECOND<\/td>\n<td>&#8216;DAYS HOURS:MINUTES:SECONDS&#8217;<\/td>\n<\/tr>\n<tr>\n<td>DAY_MINUTE<\/td>\n<td>&#8216;DAYS HOURS:MINUTES&#8217;<\/td>\n<\/tr>\n<tr>\n<td>DAY_HOUR<\/td>\n<td>&#8216;DAYS HOURS&#8217;<\/td>\n<\/tr>\n<tr>\n<td>YEAR_MONTH<\/td>\n<td>&#8216;YEARS-MONTHS&#8217;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, you can use the SUBDATE() function to subtract a specified amount of time from a date. For example, you could use it to subtract 10 days from a given date. You can specify whether to subtract days, weeks, months, quarters, years, etc. You can also subtract a time value, such as seconds, microseconds, &#8230; <a title=\"SUBDATE() Examples &#8211; MySQL\" class=\"read-more\" href=\"https:\/\/database.guide\/subdate-examples-mysql\/\" aria-label=\"Read more about SUBDATE() Examples &#8211; MySQL\">Read more<\/a><\/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-2626","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\/2626","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=2626"}],"version-history":[{"count":4,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2626\/revisions"}],"predecessor-version":[{"id":2635,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2626\/revisions\/2635"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}