DATE()
Published Aug 19, 2021Updated Sep 3, 2021
Contribute to Docs
The DATE()
function extracts just the date portion of a time string, which consists of the year, month, and day (YYYY-MM-DD).
Syntax
SELECT DATE('2022-09-01 17:38:22');-- Result: 2022-09-01
Example
For example, suppose there’s a bodega
table with the following data:
order_id | item | price | quantity | order_date |
---|---|---|---|---|
1 | Donut | 2.49 | 2 | 2022-08-16 08:04:23 |
2 | Cookie | 0.99 | 3 | 2022-08-16 09:43:00 |
3 | Donut | 2.49 | 1 | 2022-08-16 11:25:12 |
4 | Egg Sandwich | 7.99 | 1 | 2022-08-17 11:45:41 |
5 | Ice Coffee | 3.99 | 2 | 2022-08-17 12:18:50 |
SELECT DATE(order_date),COUNT(*) AS 'count_baked_goods'FROM bodegaGROUP BY DATE(order_date);
This would return the different dates from the order_date
column and the total number of rows with each date:
DATE(order_date) | count_baked_goods |
---|---|
2022-08-16 | 3 |
2022-08-17 | 2 |
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SQL on Codecademy
- Skill path
Analyze Data with SQL
Learn to analyze data with SQL and prepare for technical interviews.Includes 9 CoursesWith CertificateBeginner Friendly17 hours - Skill path
Design Databases With PostgreSQL
Learn how to query SQL databases and design relational databases to efficiently store large quantities of data.Includes 5 CoursesWith CertificateBeginner Friendly13 hours - Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly5 hours