This lesson on DATE FUNCTIONS is hands-on and example-driven. You will learn three essential SQL functions (EXTRACT, DATE_TRUNC, and DATE_DIFF) to manipulate and analyze date and timestamp data. You will be able to isolate specific date parts, standardize timestamps to a desired granularity, and calculate the duration between two points in time.
What You'll Be Able To Do
- Isolate the year, month, or hour from a full timestamp using EXTRACT.
- Standardize a timestamp to the beginning of a specified period (e.g., month or day).
- Calculate the time difference between two timestamps in minutes, hours, or seconds.
- Determine the appropriate granularity (year, quarter, day) for date manipulation functions.
- Group analytical results based on extracted or truncated date components.
Topics Covered in DATE FUNCTIONS
- Introduction to Dates (0:00 - 0:15) — Dealing with dates often requires extracting specific parts like the year from a longer date.
- EXTRACT Function (0:15 - 0:50) — The EXTRACT function pulls a specified date or time part from a date, time, or timestamp.
- EXTRACT Granularity (0:50 - 1:30) — Examples show extracting hour, day, month, year, and quarter, which can then be used for grouping.
- DATE_TRUNC Overview (1:30 - 1:55) — DATE_TRUNC truncates a date or timestamp to a specified position, standardizing it backward.
- DATE_TRUNC Examples (1:55 - 2:40) — Truncating to year sets everything else to the first of that year, while truncating to day zeroes out time components.
- DATE_DIFF Function (2:40 - 3:00) — DATE_DIFF calculates the difference between two dates, times, or timestamps.
- DATE_DIFF Calculation (3:00 - 3:45) — Using DATE_DIFF with 'minute' granularity calculates the duration between start and end timestamps, returning a numeric result.
- Granularity Summary (3:45 - 4:00) — All three functions generally support granularities like year, month, day, hour, and minute, assuming the data detail exists.
SQL Cheat Sheet
-
EXTRACT(part FROM timestamp)— Pulls specified date or time part from a date or timestampSELECT EXTRACT(HOUR FROM order_timestamp) FROM orders; -
DATE_TRUNC('part', timestamp)— Truncates a date/timestamp to the start of the specified positionSELECT DATE_TRUNC('year', order_timestamp) FROM orders; -
DATE_DIFF('part', start_ts, end_ts)— Calculates the difference between two dates or timestampsSELECT DATE_DIFF('minute', start_ts, end_ts); -
YEAR— Specifies the year component for extraction or truncationSELECT EXTRACT(YEAR FROM order_timestamp); -
QUARTER— Specifies the quarter component (1-4) for extractionSELECT EXTRACT(QUARTER FROM order_timestamp); -
DAY— Specifies the day component for extraction or truncationSELECT DATE_TRUNC('day', order_timestamp);
Comparison Table
| Function | Purpose | Output Type |
|---|---|---|
| EXTRACT | Isolates a specific date part. | Numeric value (integer). |
| DATE_TRUNC | Standardizes timestamp to period start. | Timestamp/Date object. |
| DATE_DIFF | Measures duration between two points. | Numeric value (integer). |
Common Pitfalls
- Mistake: Using DATE_TRUNC when you only need the numeric value of the part. Avoid: Use EXTRACT if you only need the number (e.g., 1 for January).
- Mistake: Expecting DATE_TRUNC to return just the year number. Avoid: DATE_TRUNC returns a full timestamp starting at that period's beginning.
- Mistake: Confusing the order of arguments in DATE_DIFF. Avoid: Always specify granularity first, then start time, then end time.
FAQs
- What is the difference between EXTRACT and DATE_TRUNC? EXTRACT returns a number representing a date part (e.g., 1 for January). DATE_TRUNC returns a full timestamp standardized to the start of that period (e.g., 2024-01-01 00:00:00).
- Can I use DATE_DIFF to find the difference in days? Yes, you can specify 'day' as the granularity argument in DATE_DIFF to calculate the difference in days.
- Do these functions work if my column is just a DATE, not a TIMESTAMP? Yes, but you cannot use granularities finer than 'day' (like hour or minute) if the underlying data lacks that detail.