Complete the code to round the timestamp to the nearest day.
SELECT DATE_TRUNC([1], '2024-06-15 13:45:30'::timestamp);
The DATE_TRUNC function rounds a timestamp to the specified precision. Using 'day' rounds to the start of the day.
Complete the code to round the timestamp to the nearest month.
SELECT DATE_TRUNC([1], '2024-06-15 13:45:30'::timestamp);
Using 'month' with DATE_TRUNC rounds the timestamp to the first day of the month at midnight.
Fix the error in the code to correctly round the timestamp to the nearest hour.
SELECT DATE_TRUNC([1], '2024-06-15 13:45:30'::timestamp);
The correct precision for rounding to the nearest hour is 'hour'. Using other values will round differently.
Fill both blanks to round the timestamp to the nearest week starting on Monday.
SELECT DATE_TRUNC([1], '2024-06-15 13:45:30'::timestamp) + INTERVAL '[2] days';
DATE_TRUNC('week', timestamp) rounds to the start of the week on Sunday. Adding 1 day shifts it to Monday.
Fill all three blanks to round the timestamp to the nearest quarter (3 months).
SELECT DATE_TRUNC([1], '2024-06-15 13:45:30'::timestamp) + INTERVAL '[2] months' * ((EXTRACT(MONTH FROM '2024-06-15'::date) - 1) / [3]);
Rounding to the nearest quarter involves truncating to the month, then adding months in multiples of 3. The divisor and multiplier are both 3.