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 Name | Type |
|---|---|
| day | date |
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
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
โ Linear Growth
Space Complexity
O(1)
Constant extra space per row
โ 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)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code