Back to Library

DATEFORMAT() in SQL

Converts a date/timestamp expression into a specified string format.

Syntax

DATE_FORMAT(date_expression, format_string)
Return type
STRING/VARCHAR

DATEFORMAT() Function Example

SELECT DATE_FORMAT(order_date, '%Y-%m-%d') FROM orders;
-- Output: '2024-12-26'

What is DATEFORMAT() in SQL?

The DATEFORMAT() function in SQL converts a date or timestamp expression into a specified string format, allowing customization of how date values are displayed. It is commonly used for reporting, user-friendly date formatting, and exporting data in specific formats.

This function is not standard SQL but is available in certain databases:

  • MySQL: Uses DATE_FORMAT(date, format)

  • SQL Server: Uses FORMAT(date, format)

  • PostgreSQL & Oracle: Use TO_CHAR(date, format)

  • SQLite: Uses strftime(format, date)

Parameters:

  • date_expression: Date, datetime, or timestamp value

  • format_string: Format pattern specifying output format

Example Use Cases:

-- Format date as 'YYYY-MM-DD'

SELECT DATE_FORMAT(order_date, '%Y-%m-%d') AS formatted_date FROM orders;
-- Output:
formatted_date
--------------
2024-12-28
2024-12-27
2024-12-26
2024-12-25
2024-12-24

-- Format with custom separators

SELECT DATE_FORMAT(event_date, '%d/%m/%Y') AS eu_date FROM events;
-- Output:
eu_date
---------
28/12/2024
27/12/2024
26/12/2024
25/12/2024
24/12/2024

-- Include time component

SELECT DATE_FORMAT(timestamp_col, '%Y-%m-%d %H:%i:%s') AS datetime FROM logs;
-- Output:
datetime
------------------------
2024-12-28 14:30:25
2024-12-28 09:15:45
2024-12-28 11:20:15
2024-12-28 13:45:30
2024-12-28 16:05:10

Notes:

  • Behavior: Returns NULL if input is NULL

  • Performance Considerations: String manipulation may impact performance

  • Version Info: Format patterns vary by DBMS

  • Deprecated/Recommended Alternatives: Some DBMS use TO_CHAR() or CONVERT()

Error Handling:

  • Error: Returns NULL for invalid dates or formats

  • Recommendation: Validate date and format string before conversion


Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

DATE_FORMAT()

DATE_FORMAT(date, '%Y-%m-%d')

Returns NULL

PostgreSQL

TO_CHAR()

TO_CHAR(date, 'YYYY-MM-DD')

Returns NULL

SQL Server

FORMAT() or CONVERT()

FORMAT(date, 'yyyy-MM-dd')

Returns NULL

SQLite

strftime()

strftime('%Y-%m-%d', date)

Returns NULL

BigQuery

FORMAT_DATE()

FORMAT_DATE('%Y-%m-%d', date)

Returns NULL

Snowflake

TO_CHAR()

TO_CHAR(date, 'YYYY-MM-DD')

Returns NULL

Athena

date_format()

date_format(date, '%Y-%m-%d')

Returns NULL

Related Functions:

DATEFROMPARTS()

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

DATEFORMAT()

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

EOMONTH()

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

DAY()

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

MONTH()

Extracts the month value (1-12) from a date or timestamp expression.

Extracts the month value (1-12) from a date or timestamp expression.

Extracts the month value (1-12) from a date or timestamp expression.

Extracts the month value (1-12) from a date or timestamp expression.

YEAR()

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

DATEDIFF()

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

DATEADD()

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

GETDATE()

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

One place for all your queries,
directly on your SQL editor