Table: Days
+-------------+------+ | Column Name | Type | +-------------+------+ | day | date | +-------------+------+ day is the column with unique values for this table.
Write a solution to convert each date in Days into a string formatted as "day_name, month_name day, year".
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Days table: +------------+ | day | +------------+ | 2022-04-12 | | 2021-08-09 | | 2020-06-26 | +------------+ Output: +-------------------------+ | day | +-------------------------+ | Tuesday, April 12, 2022 | | Monday, August 9, 2021 | | Friday, June 26, 2020 | +-------------------------+ Explanation: Please note that the output is case-sensitive.
Problem Overview: The table Days contains a column with dates stored in the standard YYYY-MM-DD format. The task is to transform each value into a human‑readable string such as DayName, MonthName DD, YYYY using SQL.
Approach 1: MySQL DATE_FORMAT Function (O(n) time, O(1) space)
The simplest solution uses MySQL’s built‑in DATE_FORMAT() function to convert the date into the required textual representation. You scan each row in the Days table and apply a formatting pattern like %W, %M %d, %Y. These format specifiers map directly to parts of the date: %W returns the weekday name, %M returns the month name, %d returns the two‑digit day, and %Y returns the four‑digit year.
The query effectively performs a full table scan and converts the value on the fly. Since formatting happens per row without additional storage or joins, the time complexity is O(n) where n is the number of records. Space complexity remains O(1) because the transformation is computed directly in the result set.
This approach is idiomatic SQL. Instead of manually extracting parts of the date using substring operations or arithmetic functions, the database engine handles localization and formatting internally. The query stays concise, readable, and efficient.
Problems like this are common when working with database queries that involve presentation formatting. Understanding built‑in date utilities in SQL is critical because they replace verbose string manipulation logic. MySQL provides many such utilities for date arithmetic, parsing, and display formatting.
Recommended for interviews: Interviewers expect the direct DATE_FORMAT() solution. It demonstrates familiarity with SQL date functions and avoids unnecessary string manipulation. A brute force approach using SUBSTRING or concatenation technically works but signals weaker knowledge of database utilities. Using the built‑in formatter shows you know how to leverage the database engine efficiently.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DATE_FORMAT Built-in Function | O(n) | O(1) | Standard solution when using MySQL. Clean and efficient formatting using native date utilities. |
| Manual String Extraction (SUBSTRING + CONCAT) | O(n) | O(1) | Rarely preferred. Useful only if database formatting functions are unavailable. |
LeetCode SQL 1853. Convert Date Format • adigolee • 97 views views
Practice Convert Date Format with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor