0
0
PostgreSQLquery~10 mins

DATE_TRUNC for rounding dates in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - DATE_TRUNC for rounding dates
Input: timestamp
Choose precision: year, month, day, hour, etc.
DATE_TRUNC function applies
Output: timestamp rounded down to chosen precision
DATE_TRUNC takes a timestamp and rounds it down to the specified precision like year, month, or day.
Execution Sample
PostgreSQL
SELECT DATE_TRUNC('month', TIMESTAMP '2024-06-15 13:45:30');
This query rounds the timestamp down to the start of the month.
Execution Table
StepInput TimestampPrecisionActionOutput Timestamp
12024-06-15 13:45:30monthRound down to start of month2024-06-01 00:00:00
22024-06-15 13:45:30dayRound down to start of day2024-06-15 00:00:00
32024-06-15 13:45:30hourRound down to start of hour2024-06-15 13:00:00
42024-06-15 13:45:30yearRound down to start of year2024-01-01 00:00:00
52024-06-15 13:45:30minuteRound down to start of minute2024-06-15 13:45:00
62024-06-15 13:45:30secondRound down to start of second2024-06-15 13:45:30
72024-06-15 13:45:30monthEnd of examplesExecution stops
💡 All examples shown; DATE_TRUNC rounds down timestamp to chosen precision.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
Input Timestamp2024-06-15 13:45:302024-06-15 13:45:302024-06-15 13:45:302024-06-15 13:45:302024-06-15 13:45:302024-06-15 13:45:302024-06-15 13:45:302024-06-15 13:45:30
Precisionmonthmonthdayhouryearminutesecondmonth
Output TimestampN/A2024-06-01 00:00:002024-06-15 00:00:002024-06-15 13:00:002024-01-01 00:00:002024-06-15 13:45:002024-06-15 13:45:30N/A
Key Moments - 3 Insights
Why does DATE_TRUNC('month', timestamp) set day and time to zero?
Because DATE_TRUNC rounds down to the start of the month, it resets day to 1 and time to 00:00:00 as shown in execution_table step 1.
Does DATE_TRUNC round up or down?
DATE_TRUNC always rounds down (truncates) to the start of the specified precision, never up, as seen in all output timestamps in execution_table.
What happens if you use 'second' precision on a timestamp with seconds?
The output stays the same because seconds are the smallest unit shown, so no change occurs, as in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output timestamp at step 3 when precision is 'hour'?
A2024-06-01 00:00:00
B2024-06-15 00:00:00
C2024-06-15 13:00:00
D2024-06-15 13:45:30
💡 Hint
Check the 'Output Timestamp' column in execution_table row for step 3.
At which step does the output timestamp become '2024-01-01 00:00:00'?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the 'year' precision in the execution_table and its output timestamp.
If you change the precision from 'month' to 'day' in step 1, what would the output timestamp be?
A2024-06-15 00:00:00
B2024-06-01 00:00:00
C2024-06-15 13:45:30
D2024-01-01 00:00:00
💡 Hint
Refer to execution_table step 2 where precision is 'day' and see the output.
Concept Snapshot
DATE_TRUNC('precision', timestamp)
Rounds timestamp down to start of given precision.
Common precisions: year, month, day, hour, minute, second.
Output resets smaller units to zero.
Useful for grouping or truncating dates.
Full Transcript
DATE_TRUNC is a PostgreSQL function that rounds a timestamp down to a specified precision like year, month, or day. For example, DATE_TRUNC('month', '2024-06-15 13:45:30') returns '2024-06-01 00:00:00'. It always truncates down, resetting smaller units to zero. This is helpful when you want to group or compare dates at a certain level of detail. The execution table shows how the output changes with different precisions, confirming the truncation behavior.