Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Customer cohorts
Summary

Instruction

Great! The EXTRACT() function is the standard SQL function, but most of the databases have their own, and usually better implemented function for that – often named DATE_PART() (in PostgreSQL; or DATEPART() in SQL Server or Oracle). In PostgreSQL, which we are using in this course, we'll deal with the function with the underscore. Take a look:

SELECT CAST(
  DATE_PART('year', CURRENT_TIMESTAMP)
  || '-'
  || DATE_PART('month', CURRENT_TIMESTAMP)
  || '-01'
  AS date
) AS current_month_start;

The syntax of the DATE_PART() function is more ... usual, isn't it? It's just the part of the date we want to get passed as a regular string, and the value we want the part from.

In practice, you use your database's version of DATE_PART() function rather than EXTRACT().

Exercise

Take a look at the template code. We've pasted the solution from the previous exercise together with the second version using the DATE_PART() function. Just run the query and observe the results.

Stuck? Here's a hint!