SQL STRFTIME()
Anonymous contributor
Published Aug 23, 2021Updated Sep 3, 2021
Contribute to Docs
The STRFTIME() function returns a formatted date, as specified in a format string.
Syntax
STRFTIME(format, timestring, modifier1, modifier2, ...)
formatis the format string.timestringis the column or the original value.- The remaining arguments are 0 or more optional modifiers to transform the time string.
The substitutions to extract each part of the date and time are the following:
%Yreturns the year (YYYY)%mreturns the month (01-12)%dreturns the day of month (01-31)%Hreturns the hour (00-23)%Mreturns the minute (00-59)%Sreturns the second (00-59)
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 order_date,STRFTIME('%H', order_date)FROM bodega;
This would return the time for the order_date column.
| order_date | STRFTIME(‘%H’, order_date) |
|---|---|
| 2022-08-16 08:04:23 | 08 |
| 2022-08-16 09:43:00 | 09 |
| 2022-08-16 11:25:12 | 11 |
| 2022-08-17 11:45:41 | 11 |
| 2022-08-17 12:18:50 | 12 |
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
- Learn to analyze data with SQL and prepare for technical interviews.
- Includes 9 Courses
- With Certificate
- Beginner Friendly.17 hours
- In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
- Beginner Friendly.5 hours