Skip to main content

Convert Date Format - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase3 min read
Practice this problem

Problem Statement

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.

Approach Overview

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.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
DATE_FORMAT Built-in FunctionO(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.

Video Solution

LeetCode 1853 Interview SQL Question with Detailed Explanation | Practice SQLEveryday Data Science3,673 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Convert Date Format easy or hard?
Convert Date Format is considered an Easy database problem with a high acceptance rate. The challenge mainly checks whether you know MySQL’s DATE_FORMAT() function and the correct format specifiers to display weekday, month name, day, and year.
Convert Date Format Python/Java solution
This problem is categorized under SQL database queries, so the primary solution is written in MySQL. If implemented in application code like Python or Java, you would parse the date using standard libraries such as datetime or SimpleDateFormat and then format it into the desired string pattern.
How to solve Convert Date Format in O(n)?
Use a SELECT query that applies the MySQL DATE_FORMAT() function to the date column. The database engine formats each row during the scan, producing strings like 'Wednesday, January 01, 2020'. Because every row is processed exactly once, the complexity remains O(n).
What is the best approach for Convert Date Format?
The best approach uses MySQL’s DATE_FORMAT() function to transform the date directly inside the query. It converts parts of a date such as weekday, month, day, and year using format specifiers like %W, %M, %d, and %Y. This solution runs in O(n) time since each row is formatted once and requires O(1) extra space.
Is Convert Date Format asked at Google/Amazon/Meta?
Database formatting questions appear frequently in SQL interview rounds at companies such as Amazon, Google, and Meta. While this exact problem may vary, candidates are often tested on SQL date functions, formatting, and built‑in utilities like DATE_FORMAT or TO_CHAR.
What data structure is used in Convert Date Format?
No specialized data structure is required. The solution relies on SQL date functions operating directly on table rows. The database engine reads each record and applies DATE_FORMAT() to produce the formatted string.
What is the time complexity of Convert Date Format?
The query performs a single scan over the Days table. Each row applies the DATE_FORMAT() function once, so the total time complexity is O(n) where n is the number of rows. No additional memory structures are created, giving O(1) space complexity.

Ready to solve this problem?

Practice Convert Date Format with our built-in code editor and test cases.

Practice on FleetCode