Learn SQL/Intermediate/Date & Time/SQL Date Formatting

SQL Date Formatting

A quick reference for the most common TO_CHAR format codes — the building blocks any custom date display is assembled from.

What & Why

Every TO_CHAR format string is built from a small set of pattern codes, combined however you need. This lesson is a reference for the most commonly used ones.

See How It Works

BUSINESS QUESTION

Marketing wants a readable start-month label beside each campaign's original date.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
SELECT
  name,
  start_date,
  TO_CHAR(start_date, 'YYYY-MM') AS start_month
FROM marketing.campaigns
ORDER BY start_date, name;
RESULT — exact output from the displayed Queryflo rows
namestart_datestart_month
Spring Launch2024-01-152024-01
Retention Webinar2024-02-102024-02
Finance Retargeting2024-03-122024-03
Enterprise Search2024-04-012024-04

The typed start_date remains beside its formatted month label.

Now You Try

Practice this concept

Marketing wants a YYYY-MM start-month label beside each original campaign start date.

Available schema
marketing

Prefix tables with marketing.table_name.

namestart_datestart_month
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario