Convert Date Format - Problem

You are working with a database table called Days that stores date information. Your task is to transform each date from the standard database format into a human-readable string format.

The Days table has the following structure:

Column NameType
daydate

Each date in the day column should be converted to the format: "day_name, month_name day, year"

Example: 2019-01-15 should become "Tuesday, January 15, 2019"

Write a SQL query to return all dates in this human-friendly format. The result can be returned in any order.

Input & Output

example_1.sql โ€” Basic Date Conversion
$ Input: Days table: +------------+ | day | +------------+ | 2019-01-15 | | 2020-02-01 | | 2021-08-20 | +------------+
โ€บ Output: +-----------------------------+ | day | +-----------------------------+ | Tuesday, January 15, 2019 | | Saturday, February 1, 2020 | | Friday, August 20, 2021 | +-----------------------------+
๐Ÿ’ก Note: Each date is converted from YYYY-MM-DD format to 'Day_Name, Month_Name Day, Year' format. The day of week is calculated and month numbers are converted to full month names.
example_2.sql โ€” Single Date
$ Input: Days table: +------------+ | day | +------------+ | 2022-12-25 | +------------+
โ€บ Output: +----------------------------+ | day | +----------------------------+ | Sunday, December 25, 2022 | +----------------------------+
๐Ÿ’ก Note: Christmas Day 2022 falls on a Sunday. The date 2022-12-25 is correctly converted to the full text format with proper day name and month name.
example_3.sql โ€” Leap Year Date
$ Input: Days table: +------------+ | day | +------------+ | 2020-02-29 | +------------+
โ€บ Output: +---------------------------+ | day | +---------------------------+ | Saturday, February 29, 2020 | +---------------------------+
๐Ÿ’ก Note: The leap day of 2020 (February 29) is correctly handled. This demonstrates that the date formatting functions properly handle special dates like leap year days.

Visualization

Tap to expand
Date Format Transformation PipelineDatabase Input2019-01-15Standard FormatBuilt-in FunctionDATE_FORMAT()'%W, %M %e, %Y'Optimized ProcessingHuman FormatTuesday, January15, 2019Ready for UsersComponent BreakdownDOWTuesdayMONJanuaryDAY15YR2019Assembly ProcessCombine with commas & spacesDatabase-Specific FunctionsMySQL: DATE_FORMAT(day, '%W, %M %e, %Y')PostgreSQL: TO_CHAR(day, 'Day, Month DD, YYYY')SQL Server: FORMAT(day, 'dddd, MMMM d, yyyy')Oracle: TO_CHAR(day, 'Day, Month DD, YYYY')
Understanding the Visualization
1
Input Processing
Database reads standard date format (YYYY-MM-DD)
2
Function Application
Built-in date formatting function processes the date
3
Component Extraction
Function extracts day name, month name, day number, and year
4
Format Assembly
Components are assembled into final human-readable format
Key Takeaway
๐ŸŽฏ Key Insight: Use built-in database date formatting functions for optimal performance and code simplicity. Each major database provides optimized functions specifically designed for this type of transformation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Linear time with very low constant factor due to optimized built-in functions

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Constant extra space per row

n
2n
โœ“ Linear Space

Constraints

  • The table contains valid dates only
  • Date range can span multiple years
  • Results can be returned in any order
  • Date format follows standard SQL date format (YYYY-MM-DD)
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 10
23.5K 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