ποΈ Date Formatting in SQL is the process of displaying date and time values in a specific, human-readable format. Different applications and regions may require dates to appear in different formats, such as YYYY-MM-DD, DD/MM/YYYY, or Month DD, YYYY.
π What is Date Formatting?
Databases internally store dates using standardized formats, but when data is displayed to users or exported to reports, it often needs to be formatted for readability. SQL provides formatting functions for converting date values into customized string representations.
Information
π― Why Use Date Formatting?
Formatting dates improves readability and ensures dates are presented in the format expected by users or external systems.
- π Display dates in user-friendly formats.
- π Generate reports with consistent date layouts.
- π Match regional date conventions.
- π Prepare dates for exports and documents.
- π Improve application presentation.
π Sample Table
| OrderID | Customer | OrderDate |
|---|---|---|
| 101 | Alice | 2026-08-15 |
| 102 | Bob | 2026-09-03 |
| 103 | Charlie | 2026-10-20 |
π Common Date Formats
| Format | Example |
|---|---|
| YYYY-MM-DD | 2026-08-15 |
| DD/MM/YYYY | 15/08/2026 |
| MM/DD/YYYY | 08/15/2026 |
| DD-Mon-YYYY | 15-Aug-2026 |
| Month DD, YYYY | August 15, 2026 |
ποΈ MySQL Date Formatting
MySQL uses the DATE_FORMAT() function.
MySQL DATE_FORMAT()
SELECT
DATE_FORMAT(OrderDate, '%d/%m/%Y') AS FormattedDate
FROM Orders;π PostgreSQL Date Formatting
PostgreSQL uses the TO_CHAR() function.
PostgreSQL TO_CHAR()
SELECT
TO_CHAR(OrderDate, 'DD/MM/YYYY') AS FormattedDate
FROM Orders;πͺ SQL Server Date Formatting
SQL Server supports FORMAT() (newer versions) and CONVERT().
SQL Server FORMAT()
SELECT
FORMAT(OrderDate, 'dd/MM/yyyy') AS FormattedDate
FROM Orders;SQL Server CONVERT()
SELECT
CONVERT(VARCHAR, OrderDate, 103) AS FormattedDate
FROM Orders;ποΈ Oracle Date Formatting
Oracle also uses the TO_CHAR() function.
Oracle TO_CHAR()
SELECT
TO_CHAR(OrderDate, 'DD-MON-YYYY') AS FormattedDate
FROM Orders;πͺΆ SQLite Date Formatting
SQLite provides formatting through the strftime() function.
SQLite strftime()
SELECT
strftime('%d/%m/%Y', OrderDate) AS FormattedDate
FROM Orders;π Common Formatting Tokens
| Meaning | Common Token Examples |
|---|---|
| Year | %Y, YYYY, yyyy |
| Month Number | %m, MM |
| Month Name | %M, Month, MON |
| Day | %d, DD |
| Hour | %H, HH24, HH |
| Minute | %i, MI, mm |
| Second | %S, SS, ss |
Important
π Display Formatted Dates
Example Output
SELECT
Customer,
OrderDate
FROM Orders;| Original Date | Formatted Output |
|---|---|
| 2026-08-15 | 15/08/2026 |
| 2026-09-03 | 03/09/2026 |
| 2026-10-20 | 20/10/2026 |
βοΈ Formatting vs Storage
| Storage | Formatting |
|---|---|
| Stores the actual date value. | Changes only how the date is displayed. |
| Uses native date/time data types. | Usually returns a text/string value. |
| Used for calculations. | Used for presentation. |
Remember
πΌ Real-World Examples
- π Generate financial reports with regional date formats.
- π Export data to CSV or PDF reports.
- π Display order dates in customer portals.
- π Show appointment schedules.
- π₯ Format patient visit dates in healthcare systems.
ποΈ Database Compatibility
| Database System | Date Formatting Function |
|---|---|
| MySQL | DATE_FORMAT() |
| PostgreSQL | TO_CHAR() |
| SQL Server | FORMAT() and CONVERT() |
| Oracle | TO_CHAR() |
| SQLite | strftime() |
β οΈ Common Mistakes
- β Assuming formatting syntax is identical across databases.
- β Storing formatted text instead of native date values.
- β Using formatted strings for sorting or calculations.
- β Mixing regional date formats, causing ambiguity.
Warning
β οΈ Best Practices
Best Practice
π Key Points to Remember
- π Date formatting changes how dates are displayed, not how they are stored.
- π Every major SQL database provides date formatting functions.
- π Formatting syntax differs among database systems.
- π Keep dates in native data types for calculations and sorting.
- π Apply formatting only when presenting data to users or generating reports.
- π Use built-in formatting functions for reliable and maintainable SQL code.