📖 Scenario: You work in a company database where dates are stored in a standard format. You want to display these dates in a friendlier way for reports.
🎯 Goal: Learn how to use the TO_CHAR function in PostgreSQL to format dates into readable strings.
📋 What You'll Learn
Create a table called events with an id column and a event_date column of type DATE.
Insert three rows with specific dates into the events table.
Create a variable or setting for the desired date format string.
Write a query that selects the id and formats event_date using TO_CHAR with the format string.
Add an ORDER BY clause to sort the results by the formatted date.
💡 Why This Matters
🌍 Real World
Formatting dates nicely is important for reports, user interfaces, and exporting data where human-friendly date formats improve readability.
💼 Career
Database developers and analysts often need to format dates for reports and applications using functions like TO_CHAR in PostgreSQL.
Progress0 / 4 steps
1
Create the events table with dates
Create a table called events with columns id as integer and event_date as date. Then insert these three rows exactly: (1, '2024-01-15'), (2, '2024-06-30'), (3, '2024-12-25').
PostgreSQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Define the date format string
Create a variable or setting called date_format and set it to the string 'FMMonth DD, YYYY' which will format dates like 'January 15, 2024'.
PostgreSQL
Hint
Use \set in psql to create a variable for the format string.
3
Write a query using TO_CHAR with the format string
Write a SELECT query that retrieves id and formats event_date using TO_CHAR(event_date, :'date_format') and alias it as formatted_date.
PostgreSQL
Hint
Use TO_CHAR(event_date, :'date_format') to format the date using the variable.
4
Sort the results by the formatted date
Add an ORDER BY clause to the previous SELECT query to sort the results by formatted_date in ascending order.
PostgreSQL
Hint
Use ORDER BY formatted_date ASC to sort the results by the formatted date.
Practice
(1/5)
1. What does the PostgreSQL function TO_CHAR do when used with a date?
easy
A. It converts the date into a formatted text string.
B. It changes the date into a number representing days.
C. It adds days to the date value.
D. It deletes the date from the database.
Solution
Step 1: Understand TO_CHAR purpose
The TO_CHAR function is used to convert dates or numbers into text with a specific format.
Step 2: Apply to date values
When used with a date, it changes the date into a string that looks like the format you choose (e.g., 'YYYY-MM-DD').
Final Answer:
It converts the date into a formatted text string. -> Option A
Quick Check:
TO_CHAR formats date to text [OK]
Hint: TO_CHAR turns dates into readable text formats [OK]
Common Mistakes:
Thinking TO_CHAR changes the date value itself
Confusing TO_CHAR with date arithmetic functions
Assuming TO_CHAR returns a number
2. Which of the following is the correct syntax to format the current date as 'YYYY-MM-DD' using TO_CHAR in PostgreSQL?
easy
A. SELECT TO_CHAR(CURRENT_DATE, "YYYY-MM-DD");
B. SELECT TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD');
C. SELECT TO_CHAR(CURRENT_DATE, YYYY-MM-DD);
D. SELECT TO_CHAR('YYYY-MM-DD', CURRENT_DATE);
Solution
Step 1: Check TO_CHAR function syntax
The correct syntax is TO_CHAR(date_value, 'format_string') where the format is a string in single quotes.
Step 2: Identify correct option
SELECT TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD'); uses the date first, then the format string in single quotes, which is correct.
Final Answer:
SELECT TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD'); -> Option B
Quick Check:
TO_CHAR(date, 'format') syntax [OK]
Hint: Format string must be in single quotes after date [OK]