{"id":3668,"date":"2024-08-23T14:53:19","date_gmt":"2024-08-23T06:53:19","guid":{"rendered":"https:\/\/five.co\/?p=3668"},"modified":"2026-04-27T10:17:11","modified_gmt":"2026-04-27T02:17:11","slug":"mysql-date-sub","status":"publish","type":"post","link":"https:\/\/preview-php85.dzine.org\/five\/blog\/mysql-date-sub\/","title":{"rendered":"A Guide to MySQL DATE_SUB"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Breakdown of MySQL <code>DATE_SUB<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When working with databases, especially those involving time-sensitive data, date and time manipulation is essential. MySQL provides several functions to make this easier, and one of the most versatile and commonly used is <code>DATE_SUB<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This function allows you to subtract a specific time interval from a date, making it a tool for managing schedules, deadlines, and time-based metrics in your applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div class=\"root-eb-toc-11pfj wp-block-essential-blocks-table-of-contents\"><div class=\"eb-parent-wrapper eb-parent-eb-toc-11pfj \"><div class=\"eb-toc-container eb-toc-11pfj  eb-toc-is-not-sticky eb-toc-not-collapsible eb-toc-initially-not-collapsed eb-toc-scrollToTop style-1 list-style-none\" data-scroll-top=\"false\" data-scroll-top-icon=\"fas fa-angle-up\" data-collapsible=\"false\" data-sticky-hide-mobile=\"false\" data-sticky=\"false\" data-scroll-target=\"scroll_to_toc\" data-copy-link=\"false\" data-editor-type=\"\" data-hide-desktop=\"false\" data-hide-tab=\"false\" data-hide-mobile=\"false\" data-itemcollapsed=\"false\" data-highlight-scroll=\"false\"><div class=\"eb-toc-header\"><h2 class=\"eb-toc-title\">Table of Contents<\/h2><\/div><div class=\"eb-toc-wrapper \" data-headers=\"[{&quot;level&quot;:2,&quot;content&quot;:&quot;Breakdown of MySQL DATE_SUB&quot;,&quot;text&quot;:&quot;Breakdown of MySQL DATE_SUB&quot;,&quot;link&quot;:&quot;breakdown-of-mysql-date_sub&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;What is MySQL DATE_SUB?&quot;,&quot;text&quot;:&quot;What is MySQL DATE_SUB?&quot;,&quot;link&quot;:&quot;what-is-mysql-date_sub&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Syntax&quot;,&quot;text&quot;:&quot;Syntax&quot;,&quot;link&quot;:&quot;syntax&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Practical Examples of DATE_SUB&quot;,&quot;text&quot;:&quot;Practical Examples of DATE_SUB&quot;,&quot;link&quot;:&quot;practical-examples-of-date_sub&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Example 1: Subtracting Days&quot;,&quot;text&quot;:&quot;Example 1: Subtracting Days&quot;,&quot;link&quot;:&quot;example-1-subtracting-days&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Example 2: Subtracting Months&quot;,&quot;text&quot;:&quot;Example 2: Subtracting Months&quot;,&quot;link&quot;:&quot;example-2-subtracting-months&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Example 3: Subtracting Years&quot;,&quot;text&quot;:&quot;Example 3: Subtracting Years&quot;,&quot;link&quot;:&quot;example-3-subtracting-years&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Common Use Cases for DATE_SUB&quot;,&quot;text&quot;:&quot;Common Use Cases for DATE_SUB&quot;,&quot;link&quot;:&quot;common-use-cases-for-date_sub&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;1. Calculating Deadlines&quot;,&quot;text&quot;:&quot;1. Calculating Deadlines&quot;,&quot;link&quot;:&quot;1-calculating-deadlines&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;2. Filtering Data in Queries&quot;,&quot;text&quot;:&quot;2. Filtering Data in Queries&quot;,&quot;link&quot;:&quot;2-filtering-data-in-queries&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;3. Generating Reports&quot;,&quot;text&quot;:&quot;3. Generating Reports&quot;,&quot;link&quot;:&quot;3-generating-reports&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;4. Scheduling and Notifications&quot;,&quot;text&quot;:&quot;4. Scheduling and Notifications&quot;,&quot;link&quot;:&quot;4-scheduling-and-notifications&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Using DATE_SUB with the Current Date&quot;,&quot;text&quot;:&quot;Using DATE_SUB with the Current Date&quot;,&quot;link&quot;:&quot;using-date_sub-with-the-current-date&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Combining DATE_SUB with Other MySQL Functions&quot;,&quot;text&quot;:&quot;Combining DATE_SUB with Other MySQL Functions&quot;,&quot;link&quot;:&quot;combining-date_sub-with-other-mysql-functions&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;1. Formatting Dates&quot;,&quot;text&quot;:&quot;1. Formatting Dates&quot;,&quot;link&quot;:&quot;1-formatting-dates&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;2. Conditional Logic&quot;,&quot;text&quot;:&quot;2. Conditional Logic&quot;,&quot;link&quot;:&quot;2-conditional-logic&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Things to Keep In Mind&quot;,&quot;text&quot;:&quot;Things to Keep In Mind&quot;,&quot;link&quot;:&quot;things-to-keep-in-mind&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;1. Always Validate Input Data&quot;,&quot;text&quot;:&quot;1. Always Validate Input Data&quot;,&quot;link&quot;:&quot;1-always-validate-input-data&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;2. Consider Time Zones&quot;,&quot;text&quot;:&quot;2. Consider Time Zones&quot;,&quot;link&quot;:&quot;2-consider-time-zones&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;3. Test Queries Thoroughly&quot;,&quot;text&quot;:&quot;3. Test Queries Thoroughly&quot;,&quot;link&quot;:&quot;3-test-queries-thoroughly&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;4. Use Appropriate Units&quot;,&quot;text&quot;:&quot;4. Use Appropriate Units&quot;,&quot;link&quot;:&quot;4-use-appropriate-units&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Common Errors and Troubleshooting&quot;,&quot;text&quot;:&quot;Common Errors and Troubleshooting&quot;,&quot;link&quot;:&quot;common-errors-and-troubleshooting&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;1. Invalid Date Format&quot;,&quot;text&quot;:&quot;1. Invalid Date Format&quot;,&quot;link&quot;:&quot;1-invalid-date-format&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;2. Unsupported Time Unit&quot;,&quot;text&quot;:&quot;2. Unsupported Time Unit&quot;,&quot;link&quot;:&quot;2-unsupported-time-unit&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Summary: MySQL&apos;s DATE_SUB&quot;,&quot;text&quot;:&quot;Summary: MySQL&apos;s DATE_SUB&quot;,&quot;link&quot;:&quot;summary-mysqls-date_sub&quot;}]\" data-visible=\"[true,true,true,true,true,true]\" data-delete-headers=\"[{&quot;label&quot;:&quot;Breakdown of MySQL DATE_SUB&quot;,&quot;value&quot;:&quot;breakdown-of-mysql-date_sub&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;What is MySQL DATE_SUB?&quot;,&quot;value&quot;:&quot;what-is-mysql-date_sub&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Syntax&quot;,&quot;value&quot;:&quot;syntax&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Practical Examples of DATE_SUB&quot;,&quot;value&quot;:&quot;practical-examples-of-date_sub&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Example 1: Subtracting Days&quot;,&quot;value&quot;:&quot;example-1-subtracting-days&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Example 2: Subtracting Months&quot;,&quot;value&quot;:&quot;example-2-subtracting-months&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Example 3: Subtracting Years&quot;,&quot;value&quot;:&quot;example-3-subtracting-years&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Common Use Cases for DATE_SUB&quot;,&quot;value&quot;:&quot;common-use-cases-for-date_sub&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1. Calculating Deadlines&quot;,&quot;value&quot;:&quot;1-calculating-deadlines&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;2. Filtering Data in Queries&quot;,&quot;value&quot;:&quot;2-filtering-data-in-queries&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;3. Generating Reports&quot;,&quot;value&quot;:&quot;3-generating-reports&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;4. Scheduling and Notifications&quot;,&quot;value&quot;:&quot;4-scheduling-and-notifications&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Using DATE_SUB with the Current Date&quot;,&quot;value&quot;:&quot;using-date_sub-with-the-current-date&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Combining DATE_SUB with Other MySQL Functions&quot;,&quot;value&quot;:&quot;combining-date_sub-with-other-mysql-functions&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;1. Formatting Dates&quot;,&quot;value&quot;:&quot;1-formatting-dates&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;2. Conditional Logic&quot;,&quot;value&quot;:&quot;2-conditional-logic&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Things to Keep In Mind&quot;,&quot;value&quot;:&quot;things-to-keep-in-mind&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1. Always Validate Input Data&quot;,&quot;value&quot;:&quot;1-always-validate-input-data&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;2. Consider Time Zones&quot;,&quot;value&quot;:&quot;2-consider-time-zones&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;3. Test Queries Thoroughly&quot;,&quot;value&quot;:&quot;3-test-queries-thoroughly&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;4. Use Appropriate Units&quot;,&quot;value&quot;:&quot;4-use-appropriate-units&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Common Errors and Troubleshooting&quot;,&quot;value&quot;:&quot;common-errors-and-troubleshooting&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1. Invalid Date Format&quot;,&quot;value&quot;:&quot;1-invalid-date-format&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;2. Unsupported Time Unit&quot;,&quot;value&quot;:&quot;2-unsupported-time-unit&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;Summary: MySQL&apos;s DATE_SUB&quot;,&quot;value&quot;:&quot;summary-mysqls-date_sub&quot;,&quot;isDelete&quot;:false}]\" data-smooth=\"true\" data-top-offset=\"\"><div class=\"eb-toc__list-wrap\"><ul class=\"eb-toc__list\"><li><a href=\"#what-is-mysql-date_sub\">What is MySQL DATE_SUB?<\/a><ul class=\"eb-toc__list\"><li><a href=\"#syntax\">Syntax<\/a><\/li><\/ul><li><a href=\"#practical-examples-of-date_sub\">Practical Examples of DATE_SUB<\/a><ul class=\"eb-toc__list\"><li><a href=\"#example-1-subtracting-days\">Example 1: Subtracting Days<\/a><li><a href=\"#example-2-subtracting-months\">Example 2: Subtracting Months<\/a><li><a href=\"#example-3-subtracting-years\">Example 3: Subtracting Years<\/a><\/li><\/ul><li><a href=\"#common-use-cases-for-date_sub\">Common Use Cases for DATE_SUB<\/a><li><a href=\"#things-to-keep-in-mind\">Things to Keep In Mind<\/a><li><a href=\"#common-errors-and-troubleshooting\">Common Errors and Troubleshooting<\/a><li><a href=\"#summary-mysqls-date_sub\">Summary: MySQL&#8217;s DATE_SUB<\/a><\/ul><\/div><\/div><\/div><\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">What is MySQL <code>DATE_SUB<\/code>?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DATE_SUB<\/code> is a MySQL function designed to subtract a specified time interval from a date. The result is returned as a new date. This function is particularly useful when you need to calculate past dates relative to a specific point in time, whether you&#8217;re working with schedules, reports, or any other time-based data.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax for <code>DATE_SUB<\/code> is straightforward:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"DATE_SUB(date, INTERVAL value unit)\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #ABB2BF\">DATE_SUB(<\/span><span style=\"color: #C678DD\">date<\/span><span style=\"color: #ABB2BF\">, INTERVAL <\/span><span style=\"color: #C678DD\">value<\/span><span style=\"color: #ABB2BF\"> unit)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>date<\/code>: The starting date from which the interval will be subtracted.<\/li>\n\n\n\n<li><code>value<\/code>: The amount of time to subtract.<\/li>\n\n\n\n<li><code>unit<\/code>: The unit of time, such as DAY, MONTH, YEAR, HOUR, MINUTE, or SECOND.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Examples of <code>DATE_SUB<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To help you understand how to use <code>DATE_SUB<\/code> effectively, let&#8217;s explore some practical examples. These examples cover different time intervals and use cases, showing the versatility of the function.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example 1: Subtracting Days<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you want to find the date that is 10 days before a given date. This is how you would do it:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DATE_SUB('2024-08-30', INTERVAL 10 DAY);\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> DATE_SUB(<\/span><span style=\"color: #98C379\">&#39;2024-08-30&#39;<\/span><span style=\"color: #ABB2BF\">, INTERVAL <\/span><span style=\"color: #D19A66\">10<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">DAY<\/span><span style=\"color: #ABB2BF\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This query subtracts 10 days from &#8216;2024-08-30&#8217;, resulting in:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"2024-08-20\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D19A66\">2024<\/span><span style=\"color: #ABB2BF\">-<\/span><span style=\"color: #D19A66\">08<\/span><span style=\"color: #ABB2BF\">-<\/span><span style=\"color: #D19A66\">20<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This example demonstrates how <code>DATE_SUB<\/code> can help calculate past dates, which can be crucial for tracking deadlines, scheduling tasks, or analyzing trends over time.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example 2: Subtracting Months<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say you need to find the date that is 3 months before a specific date:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DATE_SUB('2024-08-30', INTERVAL 3 MONTH);\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> DATE_SUB(<\/span><span style=\"color: #98C379\">&#39;2024-08-30&#39;<\/span><span style=\"color: #ABB2BF\">, INTERVAL <\/span><span style=\"color: #D19A66\">3<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">MONTH<\/span><span style=\"color: #ABB2BF\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This will return:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"2024-05-30\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D19A66\">2024<\/span><span style=\"color: #ABB2BF\">-<\/span><span style=\"color: #D19A66\">05<\/span><span style=\"color: #ABB2BF\">-<\/span><span style=\"color: #D19A66\">30<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Subtracting months is particularly useful when working with financial data, subscription services, or any application where recurring time periods are relevant.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example 3: Subtracting Years<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you need to calculate the date 5 years before a specific date, you can use the following query:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DATE_SUB('2024-08-30', INTERVAL 5 YEAR);\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> DATE_SUB(<\/span><span style=\"color: #98C379\">&#39;2024-08-30&#39;<\/span><span style=\"color: #ABB2BF\">, INTERVAL <\/span><span style=\"color: #D19A66\">5<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">YEAR<\/span><span style=\"color: #ABB2BF\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This will return:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"2019-08-30\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D19A66\">2019<\/span><span style=\"color: #ABB2BF\">-<\/span><span style=\"color: #D19A66\">08<\/span><span style=\"color: #ABB2BF\">-<\/span><span style=\"color: #D19A66\">30<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This example is useful in scenarios where you need to compare data over long periods, such as historical financial reports or long-term project tracking.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong style=\"background-color: rgb(69, 136, 216); color: rgb(241, 235, 218); font-size: calc(12px + 0.390625vw); font-weight: bold;\">MySQL DATE_SUB with a Rapid Database Builder<\/strong><\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While understanding SQL and executing efficient queries isn&#8217;t too difficult, building a complete database often requires significant SQL knowledge. This is where rapid database builders like Five come into play.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Five provides a MySQL database for your application and generates an automatic UI, making it easier to interact with your data.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With Five, you can create interactive forms, dynamic charts, and comprehensive reports that are automatically generated based on your database schema. This means you can efficiently handle complex queries, such as calculating dates using MySQL&#8217;s <code>DATE_SUB<\/code> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Five also enables you to write custom JavaScript and TypeScript functions, providing additional flexibility to implement complex business logic.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once your application is ready, Five simplifies deployment with just a few clicks, allowing you to deploy your MySQL-based application to a secure, scalable cloud infrastructure. This lets you focus on development while Five handles the intricacies of cloud deployment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019re serious about using MySQL and efficiently managing data, give Five a try.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/preview-php85.dzine.org\/five\/get-started\/\">Sign up for free access to Five\u2019s online development environment and start building your MySQL web application today.<\/a><\/p>\n\n\n\n<div style=\"background-color: #001524;\"><hr style=\"height: 5px;\" \/>\n<pre style=\"text-align: center; overflow: hidden; white-space: pre-line;\"><span style=\"color: #f1ebda; background-color: #4588d8; font-size: calc(18px + 0.390625vw);\"><strong>Build Your Database In 3 Steps<\/strong><br \/><span style=\"font-size: 14pt;\">Start Developing Today<\/span><\/span><\/pre>\n<p style=\"text-align: center;\"><a href=\"https:\/\/preview-php85.dzine.org\/five\/get-started\" target=\"_blank\" rel=\"noopener\"><button style=\"background-color: #f8b92b; border: none; color: black; padding: 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 18px; cursor: pointer; margin: 4px 2px; border-radius: 5px;\"><strong>Get Instant Access<\/strong><\/button><br \/><\/a><\/p>\n<hr style=\"height: 5px;\" \/><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Common Use Cases for <code>DATE_SUB<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>DATE_SUB<\/code> function is incredibly versatile and can be used in various scenarios. Below are some common use cases where <code>DATE_SUB<\/code> can simplify your SQL queries and database management.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Calculating Deadlines<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In project management or content scheduling, deadlines are crucial. You might need to determine when a task is due by subtracting a set number of days from the current date. For example, calculating a deadline that falls 7 days before the project end date:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DATE_SUB('2024-09-15', INTERVAL 7 DAY) AS deadline_date;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> DATE_SUB(<\/span><span style=\"color: #98C379\">&#39;2024-09-15&#39;<\/span><span style=\"color: #ABB2BF\">, INTERVAL <\/span><span style=\"color: #D19A66\">7<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">DAY<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> deadline_date;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2. Filtering Data in Queries<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DATE_SUB<\/code> is often used in the <code>WHERE<\/code> clause to filter records based on specific time frames. For example, retrieving all records from the last 30 days:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT * FROM orders WHERE order_date &gt;= DATE_SUB(CURDATE(), INTERVAL 30 DAY);\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> * <\/span><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> orders <\/span><span style=\"color: #C678DD\">WHERE<\/span><span style=\"color: #ABB2BF\"> order_date <\/span><span style=\"color: #56B6C2\">&gt;=<\/span><span style=\"color: #ABB2BF\"> DATE_SUB(CURDATE(), INTERVAL <\/span><span style=\"color: #D19A66\">30<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">DAY<\/span><span style=\"color: #ABB2BF\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This query will return all orders placed within the last 30 days, which is useful for generating recent sales reports or tracking recent user activity.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Generating Reports<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When generating reports that involve time-based data, such as monthly or quarterly performance metrics, <code>DATE_SUB<\/code> allows you to easily calculate the start or end of a reporting period. For instance, to find the date one quarter ago:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DATE_SUB(CURDATE(), INTERVAL 3 MONTH) AS quarter_start_date;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> DATE_SUB(CURDATE(), INTERVAL <\/span><span style=\"color: #D19A66\">3<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">MONTH<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> quarter_start_date;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This can be particularly useful for financial reports or tracking performance metrics over time.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. Scheduling and Notifications<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re building an application that requires sending notifications or reminders, <code>DATE_SUB<\/code> can help calculate when to send them. For example, sending a reminder email 3 days before a subscription renewal:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DATE_SUB(renewal_date, INTERVAL 3 DAY) AS reminder_date FROM subscriptions;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> DATE_SUB(renewal_date, INTERVAL <\/span><span style=\"color: #D19A66\">3<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">DAY<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> reminder_date <\/span><span style=\"color: #C678DD\">FROM<\/span><span style=\"color: #ABB2BF\"> subscriptions;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using <code>DATE_SUB<\/code> with the Current Date<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Often, you might want to subtract an interval from the current date. This can be achieved using the <code>CURDATE()<\/code> function along with <code>DATE_SUB<\/code>. Here&#8217;s an example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DATE_SUB(CURDATE(), INTERVAL 7 DAY) AS last_week;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> DATE_SUB(CURDATE(), INTERVAL <\/span><span style=\"color: #D19A66\">7<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">DAY<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> last_week;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This query subtracts 7 days from the current date, giving you the date exactly one week ago.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Combining <code>DATE_SUB<\/code> with Other MySQL Functions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DATE_SUB<\/code> can be combined with other MySQL functions to create more complex queries and operations. Here are a few examples:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Formatting Dates<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">You can combine <code>DATE_SUB<\/code> with the <code>DATE_FORMAT<\/code> function to subtract an interval and format the result:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-%d') AS formatted_date;\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">DATE_FORMAT<\/span><span style=\"color: #ABB2BF\">(DATE_SUB(CURDATE(), INTERVAL <\/span><span style=\"color: #D19A66\">1<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">MONTH<\/span><span style=\"color: #ABB2BF\">), <\/span><span style=\"color: #98C379\">&#39;%Y-%m-%d&#39;<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">AS<\/span><span style=\"color: #ABB2BF\"> formatted_date;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This query subtracts one month from the current date and formats the result as &#8216;YYYY-MM-DD&#8217;.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Conditional Logic<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use <code>DATE_SUB<\/code> in conditional logic within SQL queries. For example, you might want to update records only if a certain condition is met:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"UPDATE tasks SET status = 'overdue' WHERE due_date < DATE_SUB(CURDATE(), INTERVAL 3 DAY);\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">UPDATE<\/span><span style=\"color: #ABB2BF\"> tasks <\/span><span style=\"color: #C678DD\">SET<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">status<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #56B6C2\">=<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #98C379\">&#39;overdue&#39;<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">WHERE<\/span><span style=\"color: #ABB2BF\"> due_date <\/span><span style=\"color: #56B6C2\">&lt;<\/span><span style=\"color: #ABB2BF\"> DATE_SUB(CURDATE(), INTERVAL <\/span><span style=\"color: #D19A66\">3<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">DAY<\/span><span style=\"color: #ABB2BF\">);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This query marks tasks as overdue if they are more than 3 days past the current date.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Things to Keep In Mind<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s essential to use it correctly to avoid errors and ensure optimal performance. Here are some best practices to follow:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Always Validate Input Data<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure that the date values you pass to <code>DATE_SUB<\/code> are valid and in the correct format. Invalid dates can lead to errors or unexpected results.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Consider Time Zones<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If your application deals with users across different time zones, be mindful of how <code>DATE_SUB<\/code> interacts with time zones. You may need to adjust your queries to account for time zone differences.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Test Queries Thoroughly<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Before deploying any queries that use <code>DATE_SUB<\/code>, thoroughly test them to ensure they produce the desired results. This is especially important in critical applications where incorrect date calculations could lead to significant issues.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. Use Appropriate Units<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL supports various time units (e.g., DAY, MONTH, YEAR). Make sure you use the appropriate unit for your specific use case. For example, using DAY when you need MONTH could lead to incorrect calculations.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Common Errors and Troubleshooting<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When working with <code>DATE_SUB<\/code>, you may encounter some common errors. Here are a few and how to resolve them:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Invalid Date Format<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure that the date format you&#8217;re using is correct. MySQL typically expects dates in the &#8216;YYYY-MM-DD&#8217; format. If you use a different format, you may encounter errors.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Unsupported Time Unit<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you use an unsupported time unit in your query, MySQL will throw an error. Always check the documentation to ensure the unit you are using is valid.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary: MySQL&#8217;s <code>DATE_SUB<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL&#8217;s <code>DATE_SUB<\/code> function is a robust tool for date manipulation, allowing you to subtract various time intervals from a given date. Whether you&#8217;re managing schedules, filtering data, or generating reports, <code>DATE_SUB<\/code> can simplify your tasks and make your SQL queries more efficient.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/preview-php85.dzine.org\/five\/get-started\/\">Sign up for free access to Five\u2019s online development environment and start building your MySQL web application today.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Breakdown of MySQL DATE_SUB When working with databases, especially those involving time-sensitive data, date and time manipulation is essential. MySQL provides several functions to make this easier, and one of the most versatile and commonly used is DATE_SUB. This function allows you to subtract a specific time interval from a date, making it a tool [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":3670,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[21],"tags":[],"class_list":["post-3668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"acf":[],"uagb_featured_image_src":{"full":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/08\/Five.Co-A-Guide-to-MySQL-DATE_SUB.png",1280,720,false],"thumbnail":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/08\/Five.Co-A-Guide-to-MySQL-DATE_SUB-150x150.png",150,150,true],"medium":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/08\/Five.Co-A-Guide-to-MySQL-DATE_SUB-300x169.png",300,169,true],"medium_large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/08\/Five.Co-A-Guide-to-MySQL-DATE_SUB-768x432.png",768,432,true],"large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/08\/Five.Co-A-Guide-to-MySQL-DATE_SUB-1024x576.png",1024,576,true],"1536x1536":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/08\/Five.Co-A-Guide-to-MySQL-DATE_SUB.png",1280,720,false],"2048x2048":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/08\/Five.Co-A-Guide-to-MySQL-DATE_SUB.png",1280,720,false]},"uagb_author_info":{"display_name":"Ryan Forrester","author_link":"https:\/\/preview-php85.dzine.org\/five\/author\/f1v5_ed9_au\/"},"uagb_comment_info":0,"uagb_excerpt":"Breakdown of MySQL DATE_SUB When working with databases, especially those involving time-sensitive data, date and time manipulation is essential. MySQL provides several functions to make this easier, and one of the most versatile and commonly used is DATE_SUB. This function allows you to subtract a specific time interval from a date, making it a tool&hellip;","_links":{"self":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/3668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/comments?post=3668"}],"version-history":[{"count":1,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/3668\/revisions"}],"predecessor-version":[{"id":17662,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/3668\/revisions\/17662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media\/3670"}],"wp:attachment":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media?parent=3668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/categories?post=3668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/tags?post=3668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}