0
0
PostgreSQLquery~5 mins

DATE_TRUNC for rounding dates in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the DATE_TRUNC function do in PostgreSQL?

DATE_TRUNC rounds a timestamp or date to the specified precision, like to the nearest hour, day, or month.

Click to reveal answer
beginner
How would you round a timestamp to the start of the day using DATE_TRUNC?

Use DATE_TRUNC('day', your_timestamp). It sets the time to 00:00:00 of that day.

Click to reveal answer
intermediate
Which of these is a valid precision for DATE_TRUNC?<br>year, month, minute, second, decade

All are valid precisions for DATE_TRUNC in PostgreSQL.

Click to reveal answer
beginner
What will DATE_TRUNC('hour', '2024-06-15 14:37:22') return?

It returns 2024-06-15 14:00:00, rounding down to the start of the hour.

Click to reveal answer
intermediate
Can DATE_TRUNC be used to round dates to weeks? If yes, how?

Yes. Use DATE_TRUNC('week', your_date). It rounds to the start of the week (Sunday).

Click to reveal answer
What does DATE_TRUNC('month', '2024-06-15 10:20:30') return?
A'2024-06-01 00:00:00'
B'2024-06-15 00:00:00'
C'2024-06-30 23:59:59'
D'2024-01-01 00:00:00'
Which precision rounds to the start of the week in DATE_TRUNC?
Amonth
Bday
Cweek
Dhour
If you want to remove minutes and seconds from a timestamp, which DATE_TRUNC precision do you use?
Aday
Bhour
Cminute
Dsecond
What happens if you use DATE_TRUNC('second', '2024-06-15 10:20:30.123')?
AIt rounds to the nearest second, removing milliseconds
BIt rounds to the nearest minute
CIt returns the original timestamp unchanged
DIt causes an error
Can DATE_TRUNC be used on a date type (without time)?
ANo, it causes an error
BNo, it only works on timestamps
CYes, but only for 'year' precision
DYes, it works the same as with timestamps
Explain how DATE_TRUNC can be used to round a timestamp to the start of a day and why this might be useful.
Think about removing the time part to compare dates easily.
You got /3 concepts.
    List at least five different precisions you can use with DATE_TRUNC and describe what each one does.
    Consider common time units and how truncating affects the timestamp.
    You got /2 concepts.