From 3364460acfbf35007ece1087f60a08d3d86544e4 Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 19 Jan 2026 16:50:55 -0500 Subject: [PATCH 1/6] Add SQL Dates entry for 'ADDDATE()' --- .../concepts/dates/terms/adddate/adddate.md | 181 ++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 content/sql/concepts/dates/terms/adddate/adddate.md diff --git a/content/sql/concepts/dates/terms/adddate/adddate.md b/content/sql/concepts/dates/terms/adddate/adddate.md new file mode 100644 index 00000000000..9b78a812d91 --- /dev/null +++ b/content/sql/concepts/dates/terms/adddate/adddate.md @@ -0,0 +1,181 @@ +--- +Title: 'ADDDATE' +Description: 'Adds a specific interval to a given date.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Database' + - 'Date' + - 'Queries' + - 'SQL Server' +CatalogContent: + - 'learn-sql' + - 'paths/analyze-data-with-sql' +--- + +The **`ADDDATE`** function is an [operator](https://www.codecademy.com/resources/docs/sql/operators). It adds a time interval to a given date or datetime value and returns the result. + +## Syntax + +There are two versions of this operator. + +```pseudo +SELECT ADDDATE(date, days); +``` + +...or... + +```pseudo +SELECT ADDDATE(date, INTERVAL value unit); +``` + +**Parameters:** + +- `date` (required): the date or datetime value (see [SQL Date Formats](https://www.codecademy.com/resources/docs/sql/date-format) for more information) +- `days` (required): the number of days to add to `date` +- `unit` (required): the unit of date/time to add + - The specified unit is one of the following: + - `MICROSECOND` + - `SECOND` + - `MINUTE` + - `HOUR` + - `DAY` + - `WEEK` + - `MONTH` + - `QUARTER` + - `YEAR` + - `SECOND_MICROSECOND` + - `MINUTE_MICROSECOND` + - `MINUTE_SECOND` + - `HOUR_MICROSECOND` + - `HOUR_SECOND` + - `HOUR_MINUTE` + - `DAY_MICROSECOND` + - `DAY_SECOND` + - `DAY_MINUTE` + - `DAY_HOUR` + - `YEAR_MONTH` +- `value` (required): the value of the given date/time interval (`unit`) to add + +> Note: `ADDDATE` is specific to MySQL. A similar function exists in SQL Server under the function [`DATEADD`](https://www.codecademy.com/resources/docs/sql/dates/dateadd) which has its own set of parameters. + +## Example 1 + +Let's use the first variation of `ADDDATE`. We'll add a few days to the date `2026-01-16`. + +```sql +SELECT ADDDATE('2026-01-16', 5); + -> 2026-01-21 +``` + +We can use the second variation of `ADDDATE` to get the same result. + +```sql +SELECT ADDDATE('2026-01-16', INTERVAL 5 DAY); + -> 2026-01-21 +``` + +## Example 2 + +Given a date value, we can add one week or 7 days to achieve the same result. + +```sql +SELECT ADDDATE('2026-05-29', 7); + -> 2026-06-05 + +SELECT ADDDATE('2026-05-29', INTERVAL 1 WEEK); + -> 2026-06-05 +``` + +## Example 3 + +Given a datetime value, we can add 60 seconds or 1 minute to achieve the same result. + +```sql +SELECT ADDDATE('2026-06-12 18:00:00', INTERVAL 60 SECOND); + -> 2026-06-12 18:01:00 + +SELECT ADDDATE('2026-06-12 18:00:00', INTERVAL 1 MINUTE); + -> 2026-06-12 18:01:00 +``` + +## Example 4 + +The parameter `unit` can be one of the following composite interval expressions and their respective formats. + +| expression | format | +| -------------------- | ------------------------------------ | +| `SECOND_MICROSECOND` | `SECOND.MICROSECOND` | +| `MINUTE_MICROSECOND` | `MINUTE:SECOND.MICROSECOND` | +| `MINUTE_SECOND` | `MINUTE:SECOND` | +| `HOUR_MICROSECOND` | `HOUR:MINUTE:SECOND.MICROSECOND` | +| `HOUR_SECOND` | `HOUR:MINUTE:SECOND` | +| `HOUR_MINUTE` | `HOUR:MINUTE` | +| `DAY_MICROSECOND` | `DAY HOUR:MINUTE:SECOND.MICROSECOND` | +| `DAY_SECOND` | `DAY HOUR:MINUTE:SECOND` | +| `DAY_MINUTE` | `DAY HOUR:MINUTE` | +| `DAY_HOUR` | `DAY HOUR` | +| `YEAR_MONTH` | `YEAR-MONTH` | + +```sql +SELECT ADDDATE('2026-09-25 14:00:00', INTERVAL '7 1' DAY_HOUR); + -> 2026-10-02 15:00:00 + +SELECT ADDDATE('2026-09-25', INTERVAL '1-1' YEAR_MONTH); + -> 2027-10-25 + +SELECT ADDDATE('2026-09-25 14:00:00', INTERVAL '6:23:42' HOUR_SECOND); + -> 2026-09-25 20:23:42 + +SELECT ADDDATE('2026-09-25 16:00:00', INTERVAL '2.837' SECOND_MICROSECOND); + -> 2026-09-25 16:00:02.837000 +``` + +## Example 5 + +Consider the following list of San Diego bus routes and their arrival times. This table will be named `sd_bus_routes`. + +| ROUTE_ID | ROUTE_NAME | ARRIVAL_TIME | +| -------- | ----------------------------------------- | ------------------- | +| 1 | Fashion Valley - La Mesa | 2026-08-28 16:11:00 | +| 2 | Downtown - North Park | 2026-08-28 16:14:00 | +| 3 | UCSD Med. Ctr. / Hillcrest Euclid Trolley | 2026-08-28 16:26:00 | + +August 28, 2026 is a Friday and the timestamps are in the afternoon. Let's say all three routes will be delayed due to San Diego traffic: route 1 by 10 minutes, route 2 by 12 minutes, and route 3 by 15 minutes. First, we'll need to add a new column to this table for the updated times. + +```sql +ALTER TABLE sd_bus_routes +ADD UPDATED_ARRIVAL_TIME DATETIME; +``` + +| ROUTE_ID | ROUTE_NAME | ARRIVAL_TIME | UPDATED_ARRIVAL_TIME | +| -------- | ----------------------------------------- | ------------------- | -------------------- | +| 1 | Fashion Valley - La Mesa | 2026-08-28 16:11:00 | null | +| 2 | Downtown - North Park | 2026-08-28 16:14:00 | null | +| 3 | UCSD Med. Ctr. / Hillcrest Euclid Trolley | 2026-08-28 16:26:00 | null | + +Then, we'll update values in the new column by runing `ADDDATE` on for each respective `ARRIVAL_TIME` value. + +```sql +UPDATE sd_bus_routes +SET UPDATED_ARRIVAL_TIME = +( + CASE + WHEN ROUTE_ID = 1 + THEN ADDDATE(ARRIVAL_TIME, INTERVAL 10 MINUTE) + WHEN ROUTE_ID = 2 + THEN ADDDATE(ARRIVAL_TIME, INTERVAL 12 MINUTE) + WHEN ROUTE_ID = 3 + THEN ADDDATE(ARRIVAL_TIME, INTERVAL 15 MINUTE) + END +); +``` + +The table now looks like this: + +| ROUTE_ID | ROUTE_NAME | ARRIVAL_TIME | UPDATED_ARRIVAL_TIME | +| -------- | ----------------------------------------- | ------------------- | -------------------- | +| 1 | Fashion Valley - La Mesa | 2026-08-28 16:11:00 | 2026-08-28 16:21:00 | +| 2 | Downtown - North Park | 2026-08-28 16:14:00 | 2026-08-28 16:26:00 | +| 3 | UCSD Med. Ctr. / Hillcrest Euclid Trolley | 2026-08-28 16:26:00 | 2026-08-28 16:41:00 | From 36511dff4fe46349c7366cd752ab93f1c7cc36d7 Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 20 Jan 2026 11:47:47 -0500 Subject: [PATCH 2/6] Fix title --- .../concepts/dates/terms/adddate/adddate.md | 32 +++---------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/content/sql/concepts/dates/terms/adddate/adddate.md b/content/sql/concepts/dates/terms/adddate/adddate.md index 9b78a812d91..b78b5811534 100644 --- a/content/sql/concepts/dates/terms/adddate/adddate.md +++ b/content/sql/concepts/dates/terms/adddate/adddate.md @@ -1,5 +1,5 @@ --- -Title: 'ADDDATE' +Title: 'ADDDATE()' Description: 'Adds a specific interval to a given date.' Subjects: - 'Computer Science' @@ -14,7 +14,7 @@ CatalogContent: - 'paths/analyze-data-with-sql' --- -The **`ADDDATE`** function is an [operator](https://www.codecademy.com/resources/docs/sql/operators). It adds a time interval to a given date or datetime value and returns the result. +**`ADDDATE()`** in SQL returns a new date by adding a specified time interval (such as days, months, or years) to a given date. ## Syntax @@ -76,31 +76,9 @@ SELECT ADDDATE('2026-01-16', INTERVAL 5 DAY); -> 2026-01-21 ``` -## Example 2 - -Given a date value, we can add one week or 7 days to achieve the same result. - -```sql -SELECT ADDDATE('2026-05-29', 7); - -> 2026-06-05 - -SELECT ADDDATE('2026-05-29', INTERVAL 1 WEEK); - -> 2026-06-05 -``` - -## Example 3 +In the same vein, using `ADDDATE(date, 7)` and `ADDDATE(date, INTERVAL 1 WEEK)` will return the same result, as well as `ADDDATE(datetime, INTERVAL 60 SECOND)` and `ADDDATE(datetime, INTERVAL 1 MINUTE)`; -Given a datetime value, we can add 60 seconds or 1 minute to achieve the same result. - -```sql -SELECT ADDDATE('2026-06-12 18:00:00', INTERVAL 60 SECOND); - -> 2026-06-12 18:01:00 - -SELECT ADDDATE('2026-06-12 18:00:00', INTERVAL 1 MINUTE); - -> 2026-06-12 18:01:00 -``` - -## Example 4 +## Example 2 The parameter `unit` can be one of the following composite interval expressions and their respective formats. @@ -132,7 +110,7 @@ SELECT ADDDATE('2026-09-25 16:00:00', INTERVAL '2.837' SECOND_MICROSECOND); -> 2026-09-25 16:00:02.837000 ``` -## Example 5 +## Example 3 Consider the following list of San Diego bus routes and their arrival times. This table will be named `sd_bus_routes`. From b9aed1e9c372e164657fb062648a1c2c0b6691b9 Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 20 Jan 2026 11:48:58 -0500 Subject: [PATCH 3/6] Update introduction text --- .../concepts/dates/terms/adddate/adddate.md | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/content/sql/concepts/dates/terms/adddate/adddate.md b/content/sql/concepts/dates/terms/adddate/adddate.md index b78b5811534..c22151233c4 100644 --- a/content/sql/concepts/dates/terms/adddate/adddate.md +++ b/content/sql/concepts/dates/terms/adddate/adddate.md @@ -76,10 +76,32 @@ SELECT ADDDATE('2026-01-16', INTERVAL 5 DAY); -> 2026-01-21 ``` -In the same vein, using `ADDDATE(date, 7)` and `ADDDATE(date, INTERVAL 1 WEEK)` will return the same result, as well as `ADDDATE(datetime, INTERVAL 60 SECOND)` and `ADDDATE(datetime, INTERVAL 1 MINUTE)`; - ## Example 2 +Given a date value, we can add one week or 7 days to achieve the same result. + +```sql +SELECT ADDDATE('2026-05-29', 7); + -> 2026-06-05 + +SELECT ADDDATE('2026-05-29', INTERVAL 1 WEEK); + -> 2026-06-05 +``` + +## Example 3 + +Given a datetime value, we can add 60 seconds or 1 minute to achieve the same result. + +```sql +SELECT ADDDATE('2026-06-12 18:00:00', INTERVAL 60 SECOND); + -> 2026-06-12 18:01:00 + +SELECT ADDDATE('2026-06-12 18:00:00', INTERVAL 1 MINUTE); + -> 2026-06-12 18:01:00 +``` + +## Example 4 + The parameter `unit` can be one of the following composite interval expressions and their respective formats. | expression | format | @@ -110,7 +132,7 @@ SELECT ADDDATE('2026-09-25 16:00:00', INTERVAL '2.837' SECOND_MICROSECOND); -> 2026-09-25 16:00:02.837000 ``` -## Example 3 +## Example 5 Consider the following list of San Diego bus routes and their arrival times. This table will be named `sd_bus_routes`. From bb89565cf926106d7f4afa1995e47e1abf186f4d Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 20 Jan 2026 11:52:07 -0500 Subject: [PATCH 4/6] Remove two examples --- .../concepts/dates/terms/adddate/adddate.md | 28 ++----------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/content/sql/concepts/dates/terms/adddate/adddate.md b/content/sql/concepts/dates/terms/adddate/adddate.md index c22151233c4..f4bbfc58951 100644 --- a/content/sql/concepts/dates/terms/adddate/adddate.md +++ b/content/sql/concepts/dates/terms/adddate/adddate.md @@ -76,31 +76,9 @@ SELECT ADDDATE('2026-01-16', INTERVAL 5 DAY); -> 2026-01-21 ``` -## Example 2 - -Given a date value, we can add one week or 7 days to achieve the same result. - -```sql -SELECT ADDDATE('2026-05-29', 7); - -> 2026-06-05 - -SELECT ADDDATE('2026-05-29', INTERVAL 1 WEEK); - -> 2026-06-05 -``` - -## Example 3 +In the same vein, `ADDDATE(date, 7)` and `ADDDATE(date, INTERVAL 1 WEEK)` will return the same result, as well as both `ADDDATE(datetime, INTERVAL 60 SECOND)` and `ADDDATE(datetime, INTERVAL 1 MINUTE)`. -Given a datetime value, we can add 60 seconds or 1 minute to achieve the same result. - -```sql -SELECT ADDDATE('2026-06-12 18:00:00', INTERVAL 60 SECOND); - -> 2026-06-12 18:01:00 - -SELECT ADDDATE('2026-06-12 18:00:00', INTERVAL 1 MINUTE); - -> 2026-06-12 18:01:00 -``` - -## Example 4 +## Example 2 The parameter `unit` can be one of the following composite interval expressions and their respective formats. @@ -132,7 +110,7 @@ SELECT ADDDATE('2026-09-25 16:00:00', INTERVAL '2.837' SECOND_MICROSECOND); -> 2026-09-25 16:00:02.837000 ``` -## Example 5 +## Example 3 Consider the following list of San Diego bus routes and their arrival times. This table will be named `sd_bus_routes`. From 05d17333045c9a8847f497c44d78393df7f608ac Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 20 Jan 2026 11:55:03 -0500 Subject: [PATCH 5/6] Remove pronoun mentions --- content/sql/concepts/dates/terms/adddate/adddate.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/sql/concepts/dates/terms/adddate/adddate.md b/content/sql/concepts/dates/terms/adddate/adddate.md index f4bbfc58951..7f6f7a08e0f 100644 --- a/content/sql/concepts/dates/terms/adddate/adddate.md +++ b/content/sql/concepts/dates/terms/adddate/adddate.md @@ -62,14 +62,14 @@ SELECT ADDDATE(date, INTERVAL value unit); ## Example 1 -Let's use the first variation of `ADDDATE`. We'll add a few days to the date `2026-01-16`. +Let's use the first variation of `ADDDATE` by adding a few days to the date `2026-01-16`. ```sql SELECT ADDDATE('2026-01-16', 5); -> 2026-01-21 ``` -We can use the second variation of `ADDDATE` to get the same result. +Using the second variation of `ADDDATE` to get the same result: ```sql SELECT ADDDATE('2026-01-16', INTERVAL 5 DAY); @@ -120,7 +120,7 @@ Consider the following list of San Diego bus routes and their arrival times. Thi | 2 | Downtown - North Park | 2026-08-28 16:14:00 | | 3 | UCSD Med. Ctr. / Hillcrest Euclid Trolley | 2026-08-28 16:26:00 | -August 28, 2026 is a Friday and the timestamps are in the afternoon. Let's say all three routes will be delayed due to San Diego traffic: route 1 by 10 minutes, route 2 by 12 minutes, and route 3 by 15 minutes. First, we'll need to add a new column to this table for the updated times. +August 28, 2026 is a Friday and the timestamps are in the afternoon. Let's say all three routes will be delayed due to San Diego traffic: route 1 by 10 minutes, route 2 by 12 minutes, and route 3 by 15 minutes. First, let's add a new column to this table for the updated times. ```sql ALTER TABLE sd_bus_routes @@ -133,7 +133,7 @@ ADD UPDATED_ARRIVAL_TIME DATETIME; | 2 | Downtown - North Park | 2026-08-28 16:14:00 | null | | 3 | UCSD Med. Ctr. / Hillcrest Euclid Trolley | 2026-08-28 16:26:00 | null | -Then, we'll update values in the new column by runing `ADDDATE` on for each respective `ARRIVAL_TIME` value. +Then, let's update values in the new column by runing `ADDDATE` on for each respective `ARRIVAL_TIME` value. ```sql UPDATE sd_bus_routes From 72356a3dc567f439dfdeee50663d9b0ba630fbff Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 20 Jan 2026 12:22:02 -0500 Subject: [PATCH 6/6] Update examples and add shell enclosures for outputs --- .../concepts/dates/terms/adddate/adddate.md | 84 +++++++++++++++---- 1 file changed, 70 insertions(+), 14 deletions(-) diff --git a/content/sql/concepts/dates/terms/adddate/adddate.md b/content/sql/concepts/dates/terms/adddate/adddate.md index 7f6f7a08e0f..d719f3a75c8 100644 --- a/content/sql/concepts/dates/terms/adddate/adddate.md +++ b/content/sql/concepts/dates/terms/adddate/adddate.md @@ -62,18 +62,20 @@ SELECT ADDDATE(date, INTERVAL value unit); ## Example 1 -Let's use the first variation of `ADDDATE` by adding a few days to the date `2026-01-16`. +Let's add a few days to the date `2026-01-16`. This can be achieved using both `ADDDATE()` variations. ```sql +-- Using days = 5 SELECT ADDDATE('2026-01-16', 5); - -> 2026-01-21 + +-- Using value = 5 and unit = DAY +SELECT ADDDATE('2026-01-16', INTERVAL 5 DAY); ``` -Using the second variation of `ADDDATE` to get the same result: +Both queries will return the following output: -```sql -SELECT ADDDATE('2026-01-16', INTERVAL 5 DAY); - -> 2026-01-21 +```shell +2026-01-21 ``` In the same vein, `ADDDATE(date, 7)` and `ADDDATE(date, INTERVAL 1 WEEK)` will return the same result, as well as both `ADDDATE(datetime, INTERVAL 60 SECOND)` and `ADDDATE(datetime, INTERVAL 1 MINUTE)`. @@ -96,46 +98,98 @@ The parameter `unit` can be one of the following composite interval expressions | `DAY_HOUR` | `DAY HOUR` | | `YEAR_MONTH` | `YEAR-MONTH` | +`DAY_HOUR` example: + ```sql +-- '7 1' is 7 days and 1 hour SELECT ADDDATE('2026-09-25 14:00:00', INTERVAL '7 1' DAY_HOUR); - -> 2026-10-02 15:00:00 +``` + +```shell +2026-10-02 15:00:00 +``` -SELECT ADDDATE('2026-09-25', INTERVAL '1-1' YEAR_MONTH); - -> 2027-10-25 +`YEAR_MONTH` example: +```sql +-- '1-1' is 1 year and 1 month +SELECT ADDDATE('2026-09-25', INTERVAL '1-1' YEAR_MONTH) +``` + +```shell +2027-10-25 +``` + +`HOUR_SECOND` example: + +```sql +-- '6:23:42' is 6 hours, 23 minutes and 42 seconds SELECT ADDDATE('2026-09-25 14:00:00', INTERVAL '6:23:42' HOUR_SECOND); - -> 2026-09-25 20:23:42 +``` + +```shell +2026-09-25 20:23:42 +``` + +`SECOND_MICROSECOND` example: +```sql +-- '2.837' is 2 seconds and 837 microseconds SELECT ADDDATE('2026-09-25 16:00:00', INTERVAL '2.837' SECOND_MICROSECOND); - -> 2026-09-25 16:00:02.837000 +``` + +```shell +2026-09-25 16:00:02.837000 ``` ## Example 3 Consider the following list of San Diego bus routes and their arrival times. This table will be named `sd_bus_routes`. +```sql +-- Create table +CREATE TABLE sd_bus_routes ( + ROUTE_ID INT PRIMARY KEY, + ROUTE_NAME VARCHAR(100), + ARRIVAL_TIME DATETIME +); + +-- Insert bus routes +INSERT INTO sd_bus_routes (ROUTE_ID, ROUTE_NAME, ARRIVAL_TIME) +VALUES + (1, 'Fashion Valley - La Mesa', '2026-08-28 16:11:00'), + (2, 'Downtown - North Park', '2026-08-28 16:14:00'), + (3, 'UCSD Med. Ctr. / Hillcrest Euclid Trolley', '2026-08-28 16:26:00'); +``` + +```shell | ROUTE_ID | ROUTE_NAME | ARRIVAL_TIME | | -------- | ----------------------------------------- | ------------------- | | 1 | Fashion Valley - La Mesa | 2026-08-28 16:11:00 | | 2 | Downtown - North Park | 2026-08-28 16:14:00 | | 3 | UCSD Med. Ctr. / Hillcrest Euclid Trolley | 2026-08-28 16:26:00 | +``` -August 28, 2026 is a Friday and the timestamps are in the afternoon. Let's say all three routes will be delayed due to San Diego traffic: route 1 by 10 minutes, route 2 by 12 minutes, and route 3 by 15 minutes. First, let's add a new column to this table for the updated times. +August 28, 2026 is a Friday and the timestamps are in the afternoon, so the conditions are right for expected delays. Let's add a new column to this table to reflect this. ```sql +-- Add new column ALTER TABLE sd_bus_routes ADD UPDATED_ARRIVAL_TIME DATETIME; ``` +```shell | ROUTE_ID | ROUTE_NAME | ARRIVAL_TIME | UPDATED_ARRIVAL_TIME | | -------- | ----------------------------------------- | ------------------- | -------------------- | | 1 | Fashion Valley - La Mesa | 2026-08-28 16:11:00 | null | | 2 | Downtown - North Park | 2026-08-28 16:14:00 | null | | 3 | UCSD Med. Ctr. / Hillcrest Euclid Trolley | 2026-08-28 16:26:00 | null | +``` -Then, let's update values in the new column by runing `ADDDATE` on for each respective `ARRIVAL_TIME` value. +Let's say all three routes will be delayed due to San Diego traffic: route 1 by 10 minutes, route 2 by 12 minutes, and route 3 by 15 minutes. Let's update values in the new column by runing `ADDDATE` for each respective `ARRIVAL_TIME` value. ```sql +-- Set new arrival times UPDATE sd_bus_routes SET UPDATED_ARRIVAL_TIME = ( @@ -150,10 +204,12 @@ SET UPDATED_ARRIVAL_TIME = ); ``` -The table now looks like this: +The table now has the updated arrival times. +```shell | ROUTE_ID | ROUTE_NAME | ARRIVAL_TIME | UPDATED_ARRIVAL_TIME | | -------- | ----------------------------------------- | ------------------- | -------------------- | | 1 | Fashion Valley - La Mesa | 2026-08-28 16:11:00 | 2026-08-28 16:21:00 | | 2 | Downtown - North Park | 2026-08-28 16:14:00 | 2026-08-28 16:26:00 | | 3 | UCSD Med. Ctr. / Hillcrest Euclid Trolley | 2026-08-28 16:26:00 | 2026-08-28 16:41:00 | +```