Date Formatting in SQL

πŸ—“οΈ 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

Date formatting functions differ significantly between database systems. While the goal is the same, the function names and formatting patterns vary across MySQL, PostgreSQL, SQL Server, Oracle, and SQLite.

🎯 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

OrderIDCustomerOrderDate
101Alice2026-08-15
102Bob2026-09-03
103Charlie2026-10-20

πŸ“ Common Date Formats

FormatExample
YYYY-MM-DD2026-08-15
DD/MM/YYYY15/08/2026
MM/DD/YYYY08/15/2026
DD-Mon-YYYY15-Aug-2026
Month DD, YYYYAugust 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

MeaningCommon 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

Formatting tokens are not standardized across SQL databases. For example, MySQL uses tokens beginning with %, whereas Oracle and PostgreSQL use patterns such as YYYY and DD.

πŸ”Ž Display Formatted Dates

Example Output

SELECT
    Customer,
    OrderDate
FROM Orders;
Original DateFormatted Output
2026-08-1515/08/2026
2026-09-0303/09/2026
2026-10-2020/10/2026

βš–οΈ Formatting vs Storage

StorageFormatting
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

Formatting a date does not change the value stored in the databaseβ€”it only changes the displayed representation.

πŸ’Ό 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 SystemDate 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

Store dates using native date/time data types and apply formatting only when presenting data to users. This preserves accurate comparisons, sorting, and calculations.

⚠️ Best Practices

Best Practice

Store dates in native date/time columns, use ISO 8601 for data exchange, format dates only for display, choose regional formats appropriate for your audience, and use your database's built-in formatting functions instead of manual string manipulation.

πŸš€ 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.
>>"Store dates for accuracy, format them for people."

Summary

βœ… Date formatting allows SQL databases to present dates in user-friendly formats without changing the underlying stored values. By using the appropriate formatting functions for your database system and keeping date values in native date/time types, you can create clear reports, improve user experience, and maintain accurate date calculations.