SQL Dates and Time

Working with dates and timestamps is genuinely its own skill — this series covers reading the current moment, pulling parts out of a date, doing arithmetic, formatting for display, and filtering by relative time windows.

What & Why

Almost every real dataset has a date or timestamp column somewhere — a user signup, a campaign start date, or a lead creation time. This series covers reading the database clock, extracting calendar fields, truncating timestamps to reporting boundaries, date arithmetic, formatting, and relative windows such as the last 30 days.

See How It Works

BUSINESS QUESTION

Growth wants recent signup moments together with their calendar reporting date.

idcreated_atcountrychannelplanactivated_atchurned_at
1012024-01-15 09:10:00+00USorganicpro2024-01-16 14:25:00+00NULL
1022024-02-10 11:05:00+00CApaidfreeNULL2024-03-20 10:00:00+00
1032024-03-12 16:35:00+00GBreferralpro2024-03-13 08:15:00+00NULL
1042024-04-01 13:20:00+00USorganicfree2024-04-03 12:00:00+00NULL
EXAMPLE QUERY
SELECT
  id AS user_id,
  created_at,
  created_at::date AS signup_date
FROM growth.users
ORDER BY created_at DESC
LIMIT 20;
RESULT — exact output from the displayed Queryflo rows
user_idcreated_atsignup_date
1042024-04-01 13:20:00+002024-04-01
1032024-03-12 16:35:00+002024-03-12
1022024-02-10 11:05:00+002024-02-10
1012024-01-15 09:10:00+002024-01-15

Casting preserves the original timestamp while deriving its calendar date.

Now You Try

Practice this concept

Growth wants recent signup moments together with their calendar date.

Available schema
growth

Prefix tables with growth.table_name.

user_idcreated_atsignup_date
growth.users
ColumnType
idinteger
created_attimestamp with time zone
countrytext
channeltext
plantext
activated_attimestamp with time zone
churned_attimestamp with time zone
legacy_user_codetext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario