Back to DATA RETRIEVAL & FILTERING

DATE FUNCTIONS

Understand different `DATE` manipulation functions

5 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. Introduction to DatesDealing with dates often requires extracting specific parts like the year from a longer date.
  2. EXTRACT FunctionThe EXTRACT function pulls a specified date or time part from a date, time, or timestamp.
  3. EXTRACT GranularityExamples show extracting hour, day, month, year, and quarter, which can then be used for grouping.
  4. DATE_TRUNC OverviewDATE_TRUNC truncates a date or timestamp to a specified position, standardizing it backward.
  5. DATE_TRUNC ExamplesTruncating to year sets everything else to the first of that year, while truncating to day zeroes out time components.
  6. DATE_DIFF FunctionDATE_DIFF calculates the difference between two dates, times, or timestamps.
  7. DATE_DIFF CalculationUsing DATE_DIFF with 'minute' granularity calculates the duration between start and end timestamps, returning a numeric result.
  8. Granularity SummaryAll three functions generally support granularities like year, month, day, hour, and minute, assuming the data detail exists.
PDF notes

Frequently asked questions

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.