(5, 'Kids Story Time', '2024-03-24 11:00:00', 45);
```
---
Temporal data types are used to store date and time data. These are one of the most important data types in SQL and understanding them is crucial for working with any application that needs to store time-based data.
@ -151,96 +233,146 @@ The query might return:
Alright, now that we have seen the temporal data types, let's look at some common operations you might perform with temporal data.
## Common Operations with Temporal Data
---
Here are some common operations you might perform with temporal data in a bookstore:
## Common Temporal Operations
### Difference between two dates
I have created the following tables with sample data so that you can follow along with the examples below in the editor:
In MySQL, you can use the `DATEDIFF` function to calculate the difference between two dates. For example, given the following table:
| 1 | The Great Adventure | 2023-05-15 | 2024-01-01 |
> You don't need to memorize the functions below, it's enough to know that these operations exist and you can refer to the documentation when you need them.
>
> The goal of this section is to give you an idea of how powerful temporal data types are and how you can use them to perform various operations.
### Extracting Parts of Dates
You can use the following query to calculate how long books have been in print:
You can extract specific parts of a date like year, month, day, etc:
```sql
-- Get year from a date
SELECT
sale_date,
EXTRACT(YEAR FROM sale_date) as sale_year
FROM sales;
-- Get month number
SELECT
sale_date,
EXTRACT(MONTH FROM sale_date) as month_num
FROM sales;
-- Get day of month
SELECT
sale_date,
EXTRACT(DAY FROM sale_date) as day_of_month
FROM sales;
```
### Date Arithmetic
You can perform various arithmetic operations with dates:
```sql
-- Add or subtract intervals
SELECT
order_time,
order_time + INTERVAL '7 days' as delivery_due_date,
order_time + INTERVAL '1 month' as payment_due_date
FROM orders;
-- Calculate age or duration
SELECT
title,
publication_date,
reprint_date,
DATEDIFF(reprint_date, publication_date) as days_in_print
AGE(CURRENT_DATE, publication_date) as book_age
FROM books;
```
The output will be:
### Date Range Queries
| id | title | publication_date | reprint_date | days_in_print |
You can use the following query to get the month of sales:
Sometimes you might want to truncate the date to a specific part of the date. For example, you might want to get the start of the week, month or year for a given date.
```sql
-- Truncate to start of week
SELECT
customer_name,
DATE_FORMAT(sale_date, '%M') as month,
sale_amount
sale_date,
DATE_TRUNC('week', sale_date) as week_start
FROM sales;
```
The output will be:
| customer_name | month | sale_amount |
| ------------- | -------- | ----------- |
| John Doe | January | 100.00 |
| Jane Smith | February | 150.00 |
| Alice Johnson | March | 200.00 |
| Bob Brown | April | 120.00 |
| Carol Green | May | 180.00 |
### Getting Sales for a Specific Dates
You can use the `WHERE` clause and perform range queries on the date column. For example, given the following table:
-- Truncate to start of month
SELECT
transaction_date,
DATE_TRUNC('month', transaction_date) as month_start
You can use the following query to get the sales for March 2024:
You can use `CURRENT_DATE` and `CURRENT_TIME` to get the current date and time.
```sql
-- Get current timestamp in different formats
SELECT
customer_name,
sale_amount
FROM sales
WHERE sale_date BETWEEN '2024-03-01' AND '2024-03-31';
CURRENT_TIMESTAMP,
CURRENT_DATE,
CURRENT_TIME;
```
The output will be:
### Filtering Based on Time Parts
You can filter records based on specific parts of datetime values:
| customer_name | sale_amount |
| ------------- | ----------- |
| Alice Johnson | 200.00 |
| Bob Brown | 120.00 |
| Carol Green | 180.00 |
```sql
-- Get orders during business hours
SELECT *
FROM orders
WHERE EXTRACT(HOUR FROM order_time) BETWEEN 9 AND 17;
-- Get weekend orders
SELECT *
FROM orders
WHERE EXTRACT(DOW FROM order_time) IN (0, 6); -- Sunday = 0, Saturday = 6
-- Get morning orders (before noon)
SELECT *
FROM orders
WHERE EXTRACT(HOUR FROM order_time) <12;
```
> Note: The exact syntax for temporal operations might vary between different database systems. Always consult your specific database's documentation for the most accurate information. Here is the [MySQL documentation](https://dev.mysql.com/doc/en/date-and-time-functions.html) and [PostgreSQL documentation](https://www.postgresql.org/docs/current/functions-datetime.html).