Handling Dates Part 3
Part of the Fundamentals section of Coddy's SQL journey — lesson 33 of 72.
SQL provides several useful functions to manipulate and format dates. Here are some common date functions:
DATE()- Converts a string to a dateSTRFTIME()- Formats dates according to specified parametersDATE('now')- Gets current date:
For example convert string to date:
SELECT DATE('2023-05-15 13:45:00')
-- Returns: 2023-05-15
SELECT DATE('now')
-- Returns current dateFor example extract year, month and day from date:
-- Get year
SELECT STRFTIME('%Y', '2023-05-15')
-- Returns: 2023
-- Get month
SELECT STRFTIME('%m', '2023-05-15')
-- Returns: 05
-- Get day
SELECT STRFTIME('%d', '2023-05-15')
-- Returns: 15
-- Show combination of year and day (any separator can be used)
SELECT STRFTIME('%Y:%d', '2023-05-15')
-- Returns: 2023:15
-- Use a dash separator instead
SELECT STRFTIME('%d-%m-%Y', '2023-05-15')
-- Returns: 15-05-2023
-- Get day of week
SELECT STRFTIME('%w', '2023-05-15')
-- Returns: 1 (Monday; Sunday is 0)Common format specifiers:
- %Y: Year (4 digits)
- %m: Month (01-12)
- %d: Day of month (01-31)
- %w: Day of week (0-6, Sunday is 0)
- %H: Hour (00-23)
- %M: Minute (00-59)
- %S: Second (00-59)
You can use any separator character between specifiers (e.g. -, :, /). For example, STRFTIME('%d-%m-%Y', date) and STRFTIME('%d:%m:%Y', date) both combine day, month, and year — just with different separators.
You can combine these functions in queries:
-- Find all records from current year
SELECT * FROM table1
WHERE STRFTIME('%Y', date_column) = STRFTIME('%Y', 'now')Challenge
EasyAvailable tables and columns:
<strong>events</strong>:<strong>id</strong>,<strong>start</strong>,<strong>end</strong>
Write a query that shows:
- The event ID
- The start date formatted as "DD-MM-YYYY". Call this column
formatted_start - The end date formatted as "DD-MM-YYYY". Call this column
formatted_end - The duration in days between start and end, rounded to the nearest whole number. Call this column
duration_days
Filter only the rows that the year of the start column is 2009 (STRFTIME() returns a string and not a number).
Sort the results by the duration days in descending order.
Reminder: To calculate the difference between dates use: JULIANDAY(end) - JULIANDAY(start) and use ROUND() to round to whole numbers.
Cheat sheet
SQL provides several useful functions to manipulate and format dates:
DATE()- Converts a string to a dateSTRFTIME()- Formats dates according to specified parametersDATE('now')- Gets current date
Convert string to date:
SELECT DATE('2023-05-15 13:45:00')
-- Returns: 2023-05-15
SELECT DATE('now')
-- Returns current dateExtract date parts using STRFTIME():
-- Get year
SELECT STRFTIME('%Y', '2023-05-15')
-- Returns: 2023
-- Get month
SELECT STRFTIME('%m', '2023-05-15')
-- Returns: 05
-- Get day
SELECT STRFTIME('%d', '2023-05-15')
-- Returns: 15
-- Show combination of year and day
SELECT STRFTIME('%Y:%d', '2023-05-15')
-- Returns: 2023:15Common format specifiers:
- %Y: Year (4 digits)
- %m: Month (01-12)
- %d: Day of month (01-31)
- %H: Hour (00-23)
- %M: Minute (00-59)
- %S: Second (00-59)
Use in WHERE clauses:
-- Find all records from current year
SELECT * FROM table1
WHERE STRFTIME('%Y', date_column) = STRFTIME('%Y', 'now')Calculate date differences using JULIANDAY():
JULIANDAY(end_date) - JULIANDAY(start_date)Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns