How to Format Dates in Redshift in PostgreSQL

Galaxy Glossary

How do I format dates in Amazon Redshift SQL?

Use TO_CHAR, DATE_TRUNC, and format specifiers to display Redshift dates and timestamps in any layout.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

How do I format a date to a string in Redshift?

Call TO_CHAR(date_or_timestamp, 'format_pattern'). The first argument is a DATE, TIMESTAMP, or TIMESTAMPTZ column or expression; the second is a text literal containing PostgreSQL-style format specifiers.

What are the most common format patterns?

Use YYYY (4-digit year), MM (2-digit month), DD, HH24, MI, SS, and text like "-" or ":" to build custom strings. Example: 'YYYY-MM-DD' creates ISO dates; 'Dy, Mon DD' shows abbreviated names.

How can I truncate a date to month, week, or day?

Use DATE_TRUNC('part', timestamp). Valid parts include year, quarter, month, week, day, hour, and minute. The function returns a TIMESTAMP set to the lower precision.

Which format should I store dates in?

Store dates as native DATE or TIMESTAMP columns. Format only when selecting data for reports, exporting, or interfacing with APIs.

How do I format order dates for a CSV export?

Wrap the date column with TO_CHAR: TO_CHAR(o.order_date, 'YYYY-MM-DD'). Combine with other columns to build a ready-to-load CSV.

How do I cast a string to DATE with a custom layout?

Call TO_DATE(text_value, 'format_pattern'). Example: TO_DATE('31/12/2024','DD/MM/YYYY') parses European style dates into a proper DATE.

What about timestamps and time zones?

Redshift stores TIMESTAMP as UTC. Use CONVERT_TIMEZONE('UTC','America/Los_Angeles', ts) before formatting to display local time, or add TIMESTAMPTZ in TO_CHAR patterns.

How do I display the weekday and hour for order analysis?

Example: TO_CHAR(o.order_date, 'Dy') AS weekday, TO_CHAR(o.order_date, 'HH24') AS hour quickly yields categorical fields for funnel or cohort charts.

Best practice checklist

1) Keep data typed, format only on SELECT. 2) Always quote the pattern string. 3) Use ISO YYYY-MM-DD for interchange. 4) Test edge cases (leap days, DST shifts).

Why How to Format Dates in Redshift in PostgreSQL is important

How to Format Dates in Redshift in PostgreSQL Example Usage


-- Format each order date as ISO string and weekday name
SELECT o.id,
       TO_CHAR(o.order_date, 'YYYY-MM-DD')      AS order_date_iso,
       TO_CHAR(o.order_date, 'Dy')              AS weekday,
       c.name                                   AS customer_name,
       o.total_amount
FROM Orders o
JOIN Customers c ON c.id = o.customer_id
ORDER BY o.order_date DESC
LIMIT 10;

How to Format Dates in Redshift in PostgreSQL Syntax


TO_CHAR(date_or_timestamp, 'format_pattern')
TO_DATE(text, 'format_pattern')
DATE_TRUNC('part', timestamp)
CONVERT_TIMEZONE('from_tz','to_tz', timestamp)
-- Ecommerce context examples
SELECT TO_CHAR(o.order_date,'YYYY-MM-DD') AS order_day
FROM Orders o;

SELECT DATE_TRUNC('month', o.order_date) AS month_start, SUM(o.total_amount)
FROM Orders o
GROUP BY 1;

Common Mistakes

Frequently Asked Questions (FAQs)

Can I localize month or day names?

Redshift defaults to the database locale. Use SET lc_time = 'en_US' (session-scope) to switch language before calling TO_CHAR.

Why does DATE_TRUNC return a timestamp?

DATE_TRUNC always outputs TIMESTAMP. Cast to DATE if needed: DATE_TRUNC('month', order_date)::date.

Want to learn about other SQL terms?