{"id":2629,"date":"2018-06-20T21:33:26","date_gmt":"2018-06-21T04:33:26","guid":{"rendered":"https:\/\/database.guide\/?p=2629"},"modified":"2021-01-28T14:11:23","modified_gmt":"2021-01-28T04:11:23","slug":"date_sub-examples-mysql","status":"publish","type":"post","link":"https:\/\/database.guide\/date_sub-examples-mysql\/","title":{"rendered":"DATE_SUB() Examples &#8211; MySQL"},"content":{"rendered":"<p>In <a href=\"https:\/\/database.guide\/what-is-mysql\/\">MySQL<\/a>, you can use the <code>DATE_SUB()<\/code> function to subtract a specified amount of time from a date. For example, you can use it to subtract 7 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>This function is similar to\u00a0<a href=\"https:\/\/database.guide\/date_add-examples-mysql\/\"><code>DATE_ADD()<\/code><\/a>, except that it subtracts from a date instead of adds to it.<\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>The syntax goes like this:<\/p>\n<pre>DATE_SUB(date,INTERVAL expr unit)<\/pre>\n<h2>Example 1 &#8211; Basic Usage<\/h2>\n<p>Here&#8217;s an example of usage.<\/p>\n<pre>SELECT DATE_SUB('2021-05-07', INTERVAL 5 DAY) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+------------+\r\n| Result     |\r\n+------------+\r\n| 2021-05-02 |\r\n+------------+<\/pre>\n<p>This example subtracts 5 days from the date supplied by the first argument.<\/p>\n<h2>Example 2 &#8211; Other Date Units<\/h2>\n<p>You can specify the units in days, weeks, months, years, etc. Here are some examples.<\/p>\n<pre>SELECT \r\n    '2021-05-07' AS 'Start Date',\r\n    DATE_SUB('2021-05-07', INTERVAL 2 WEEK) AS '-2 Weeks',\r\n    DATE_SUB('2021-05-07', INTERVAL 2 MONTH) AS '-2 Months',\r\n    DATE_SUB('2021-05-07', INTERVAL 2 QUARTER) AS '-2 Quarters',\r\n    DATE_SUB('2021-05-07', INTERVAL 2 YEAR) AS '-2 Years';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+------------+------------+------------+-------------+------------+\r\n| Start Date | -2 Weeks   | -2 Months  | -2 Quarters | -2 Years   |\r\n+------------+------------+------------+-------------+------------+\r\n| 2021-05-07 | 2021-04-23 | 2021-03-07 | 2020-11-07  | 2019-05-07 |\r\n+------------+------------+------------+-------------+------------+<\/pre>\n<h2>Example 3 &#8211; Time Units<\/h2>\n<p>You can also subtract time units from a date\/time value. Here&#8217;s an example.<\/p>\n<pre>SELECT DATE_SUB('2021-05-07 10:00:00', INTERVAL 5 HOUR) AS Result;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+---------------------+\r\n| Result              |\r\n+---------------------+\r\n| 2021-05-07 05:00:00 |\r\n+---------------------+<\/pre>\n<p>And you can specify multiple units at the same time. For example, you can specify hours and minutes. Like this.<\/p>\n<pre>SELECT DATE_SUB('2021-05-07 10:00:00', INTERVAL '1:30' HOUR_MINUTE) AS Result;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+---------------------+\r\n| Result              |\r\n+---------------------+\r\n| 2021-05-07 08:30: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<p>You can also use the\u00a0<code>SUBDATE()<\/code> function to do the same thing (it&#8217;s a synonym for the <code>DATE_SUB()<\/code> function when using the same syntax).<\/p>\n<p>In addition, the syntax of <code>SUBDATE()<\/code>\u00a0has a second form, which is a shorthand method of subtracting a certain number of days from a date. For more info, see <a href=\"https:\/\/database.guide\/subdate-examples-mysql\/\">SUBDATE() Examples in MySQL<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, you can use the DATE_SUB() function to subtract a specified amount of time from a date. For example, you can use it to subtract 7 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=\"DATE_SUB() Examples &#8211; MySQL\" class=\"read-more\" href=\"https:\/\/database.guide\/date_sub-examples-mysql\/\" aria-label=\"Read more about DATE_SUB() 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-2629","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\/2629","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=2629"}],"version-history":[{"count":4,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2629\/revisions"}],"predecessor-version":[{"id":2633,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2629\/revisions\/2633"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}