-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add SQL Dates entry for 'ADDDATE()' #8180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
K5qu4r3d
wants to merge
8
commits into
Codecademy:main
Choose a base branch
from
K5qu4r3d:8170-sql-adddate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3364460
Add SQL Dates entry for 'ADDDATE()'
K5qu4r3d d6d5852
Merge branch 'main' into 8170-sql-adddate
K5qu4r3d 36511df
Fix title
K5qu4r3d b9aed1e
Update introduction text
K5qu4r3d bb89565
Remove two examples
K5qu4r3d 05d1733
Remove pronoun mentions
K5qu4r3d 72356a3
Update examples and add shell enclosures for outputs
K5qu4r3d 9cfb220
Merge branch 'main' into 8170-sql-adddate
K5qu4r3d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| --- | ||
| 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' | ||
| --- | ||
|
|
||
| **`ADDDATE()`** in SQL returns a new date by adding a specified time interval (such as days, months, or years) to a given date. | ||
|
|
||
| ## 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 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); | ||
|
|
||
| -- Using value = 5 and unit = DAY | ||
| SELECT ADDDATE('2026-01-16', INTERVAL 5 DAY); | ||
| ``` | ||
|
|
||
| Both queries will return the following output: | ||
|
|
||
| ```shell | ||
| 2026-01-21 | ||
| ``` | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Attach the output separately
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved in commit 72356a3. |
||
| 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)`. | ||
|
|
||
| ## Example 2 | ||
|
|
||
| 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` | | ||
|
|
||
| `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); | ||
| ``` | ||
|
|
||
| ```shell | ||
| 2026-10-02 15:00:00 | ||
| ``` | ||
|
|
||
| `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); | ||
| ``` | ||
|
|
||
| ```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); | ||
| ``` | ||
|
|
||
| ```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, 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 | | ||
| ``` | ||
|
|
||
| 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 = | ||
| ( | ||
| 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 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 | | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to attach the output separately wrapped in a
shellblockThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved in commit 72356a3.