Convert Date Format - Problem

You are given a table Days containing dates. Write a SQL solution to convert each date into a formatted string.

Table: Days

Column NameType
daydate

The day column contains unique date values.

Task: Convert each date to the format "day_name, month_name day, year" (e.g., "Friday, January 15, 2021").

Return the result table in any order.

Table Schema

Days
Column Name Type Description
day PK date Date value to be formatted
Primary Key: day
Note: Each row contains a unique date that needs to be converted to a specific string format

Input & Output

Example 1 — Basic Date Formatting
Input Table:
day
2022-05-08
2021-08-09
2020-06-26
Output:
day
Sunday , May 08, 2022
Monday , August 09, 2021
Friday , June 26, 2020
💡 Note:

Each date is converted to the format "day_name, month_name day, year". Note that PostgreSQL's TO_CHAR function pads day and month names with spaces to maintain consistent width.

Example 2 — Different Years and Months
Input Table:
day
2019-12-31
2023-01-01
Output:
day
Tuesday , December 31, 2019
Sunday , January 01, 2023
💡 Note:

The formatting works consistently across different years, including year boundaries like New Year's Eve and New Year's Day.

Constraints

  • day contains valid date values
  • day values are unique
  • Result can be returned in any order

Visualization

Tap to expand
Convert Date Format INPUT Days Table day (DATE) 2022-04-12 2021-08-09 2020-06-26 Date values in YYYY-MM-DD format ALGORITHM STEPS 1 Parse Date Extract year, month, day 2 Get Day Name Use DAYNAME() function 3 Get Month Name Use MONTHNAME() function 4 Concatenate Format with CONCAT() SQL Query: SELECT CONCAT( DAYNAME(day), ', ', MONTHNAME(day), ' ', DAY(day), ', ', YEAR(day)) FINAL RESULT Formatted Output day (VARCHAR) Tuesday, April 12, 2022 Monday, August 9, 2021 Friday, June 26, 2020 OK Dates Formatted! Output Format: day_name, month_name day, year Key Insight: SQL provides built-in date functions like DAYNAME(), MONTHNAME(), DAY(), and YEAR() that extract components from DATE values. Use CONCAT() to combine them into the required format. TutorialsPoint - Convert Date Format | Optimal Solution
Asked in
Amazon 15 Microsoft 12 Google 8
22.3K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen