BaşlayınÜcretsiz Başlayın

Using date functions on strings

There are several useful functions that act specifically on date or datetime fields. For example:

  • DATE_TRUNC('month', date) truncates each date to the first day of the month.
  • DATE_PART('year', date) outputs the year, as an integer, of each date value.

In general, the arguments for both functions are ('period', field), where period is a date or time interval, such as 'minute', 'day', or 'decade'.

In this exercise, your goal is to test out these date functions on the country_stats table, specifically by outputting the decade of each year using two separate approaches. To run these functions, you will need to use CAST() function on the year field.

Bu egzersiz

Reporting in SQL

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Pulling from the country_stats table, select the decade using two methods: DATE_PART() and DATE_TRUNC.
  • Convert the data type of the year field to fix errors.
  • Sum up gdp to get world_gdp.
  • Group and order by year (in descending order).

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

SELECT 
	year,
    -- Pull decade, decade_truncate, and the world's gdp
    DATE_PART(____) AS decade,
    DATE_TRUNC(____) AS decade_truncated,
    ____ AS world_gdp
FROM ____
-- Group and order by year in descending order
GROUP BY ____
ORDER BY ____;
Kodu Düzenle ve Çalıştır