(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.
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.
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:
| `books` | `id`, `title`, `publication_date`, `reprint_date` | Tracks books with their publication and reprint dates |
| `orders` | `id`, `book_id`, `order_time`, `delivery_date` | Records customer orders with order time and delivery dates |
| `sales` | `id`, `book_id`, `sale_date`, `sale_amount` | Daily sales records with transaction dates |
| `transactions` | `id`, `type`, `transaction_date`, `amount` | Financial transactions including purchases and refunds |
| `events` | `id`, `name`, `event_time`, `duration` | Bookstore events like book signings and reading clubs |
| id | title | publication_date | reprint_date |
> 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.
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
```sql
-- Truncate to start of week
SELECT
SELECT
customer_name,
sale_date,
DATE_FORMAT(sale_date, '%M') as month,
DATE_TRUNC('week', sale_date) as week_start
sale_amount
FROM sales;
FROM sales;
```
The output will be:
-- Truncate to start of month
SELECT
| customer_name | month | sale_amount |
transaction_date,
| ------------- | -------- | ----------- |
DATE_TRUNC('month', transaction_date) as month_start
| John Doe | January | 100.00 |
FROM transactions;
| 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:
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
```sql
-- Get current timestamp in different formats
SELECT
SELECT
customer_name,
CURRENT_TIMESTAMP,
sale_amount
CURRENT_DATE,
FROM sales
CURRENT_TIME;
WHERE sale_date BETWEEN '2024-03-01' AND '2024-03-31';
```
```
The output will be:
### Filtering Based on Time Parts
You can filter records based on specific parts of datetime values:
| customer_name | sale_amount |
```sql
| ------------- | ----------- |
-- Get orders during business hours
| Alice Johnson | 200.00 |
SELECT *
| Bob Brown | 120.00 |
FROM orders
| Carol Green | 180.00 |
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).
> 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).