SQL Date Format Guide — Format Dates in MySQL, PostgreSQL, SQL Server & Oracle

Published: August 4, 2026 | Updated: August 4, 2026

Getting SQL date format right is one of the most common challenges developers face. Every database engine has its own functions for formatting dates, converting strings to dates, and handling regional formats like dd/mm/yyyy. If you have ever searched for "sql datetime format" or "how to cast a string to date in SQL", this guide covers every database you need.

Below you will find a complete reference for date format in SQL Server, date format Oracle, MySQL DATE_FORMAT, and PostgreSQL TO_CHAR — all with copy-paste examples.

Table of Contents
  1. MySQL DATE_FORMAT — Format Dates and Datetimes
  2. PostgreSQL TO_CHAR — Date Formatting
  3. SQL Server Date Format — FORMAT and CONVERT
  4. Oracle Date Format — TO_CHAR and NLS
  5. Convert String to Date Across Databases
  6. Date Format Best Practices
  7. FAQ

1. MySQL DATE_FORMAT — Format Dates and Datetimes

MySQL uses the DATE_FORMAT() function to format sql datetime format values. It takes a date/datetime expression and a format string made of specifiers.

Common MySQL DATE_FORMAT Specifiers

SpecifierDescriptionExample Output
%Y4-digit year2026
%y2-digit year26
%mMonth (01-12)08
%MFull month nameAugust
%bAbbreviated monthAug
%dDay of month (01-31)04
%H24-hour hour14
%iMinutes30
%sSeconds45
%WFull weekday nameTuesday

MySQL Date Format Examples

-- ISO date: 2026-08-04
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d');

-- dd/mm/yyyy format
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y');
-- Result: 04/08/2026

-- Full readable date
SELECT DATE_FORMAT(NOW(), '%W, %M %d, %Y');
-- Result: Tuesday, August 04, 2026

-- Date and time with 12-hour clock
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %h:%i:%s %p');
-- Result: 2026-08-04 02:30:45 PM

-- Month abbreviation + 2-digit year
SELECT DATE_FORMAT(NOW(), '%b %y');
-- Result: Aug 26
MySQL tip: DATE_FORMAT works with both DATE and DATETIME columns. If you pass a DATE (no time), the time specifiers (%H, %i, %s) return 00.

2. PostgreSQL TO_CHAR — Date Formatting

PostgreSQL uses TO_CHAR() for all date and timestamp formatting. The format patterns are similar to Oracle's but with some PostgreSQL-specific additions.

PostgreSQL TO_CHAR Patterns

PatternDescriptionExample Output
YYYY4-digit year2026
YY2-digit year26
MMMonth (01-12)08
MonthFull month nameAugust
MonAbbreviated monthAug
DDDay of month (01-31)04
DayFull weekday nameTuesday
HH2424-hour hour14
MIMinutes30
SSSeconds45

PostgreSQL Date Format Examples

-- ISO date
SELECT TO_CHAR(NOW(), 'YYYY-MM-DD');
-- Result: 2026-08-04

-- dd/mm/yyyy (European format)
SELECT TO_CHAR(NOW(), 'DD/MM/YYYY');
-- Result: 04/08/2026

-- Full text date
SELECT TO_CHAR(NOW(), 'Day, Month DD, YYYY');
-- Result: Tuesday , August  04, 2026

-- Timestamp with timezone
SELECT TO_CHAR(NOW(), 'YYYY-MM-DD HH24:MI:SS TZ');
-- Result: 2026-08-04 14:30:45 UTC

-- Quarter and day of year
SELECT TO_CHAR(NOW(), 'YYYY-Q') AS quarter,
       TO_CHAR(NOW(), 'DDD') AS day_of_year;
-- Result: 2026-3 | 216

3. SQL Server Date Format — FORMAT and CONVERT

Date format in SQL Server has two main approaches: the modern FORMAT() function (SQL Server 2012+) and the legacy CONVERT() with style codes. FORMAT uses .NET format strings and is more flexible, while CONVERT is faster for pre-defined styles.

SQL Server FORMAT Function

-- ISO date
SELECT FORMAT(GETDATE(), 'yyyy-MM-dd');
-- Result: 2026-08-04

-- dd/mm/yyyy (UK/European style)
SELECT FORMAT(GETDATE(), 'dd/MM/yyyy');
-- Result: 04/08/2026

-- Full date and time
SELECT FORMAT(GETDATE(), 'dddd, MMMM dd, yyyy HH:mm:ss');
-- Result: Tuesday, August 04, 2026 14:30:45

-- Custom: year-month only
SELECT FORMAT(GETDATE(), 'yyyy-MM');
-- Result: 2026-08

SQL Server CONVERT Style Codes

StyleFormatExample
101mm/dd/yyyy08/04/2026
103dd/mm/yyyy04/08/2026
112yyyymmdd (ISO)20260804
120yyyy-mm-dd hh:mi:ss2026-08-04 14:30:45
106dd mon yyyy04 Aug 2026
-- Using CONVERT with style
SELECT CONVERT(varchar, GETDATE(), 103) AS uk_date;
-- Result: 04/08/2026

SELECT CONVERT(varchar, GETDATE(), 112) AS iso_date;
-- Result: 20260804

SELECT CONVERT(varchar, GETDATE(), 120) AS datetime_iso;
-- Result: 2026-08-04 14:30:45
Performance note: FORMAT() is more readable and flexible but slower than CONVERT() on large datasets. For formatting millions of rows, prefer CONVERT() with style codes or do formatting in your application layer.

4. Oracle Date Format — TO_CHAR and NLS

Date format Oracle relies on the TO_CHAR() function with format masks. Oracle also respects the session-level NLS_DATE_FORMAT parameter, which controls the default display format.

Oracle TO_CHAR Format Masks

-- ISO date
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') FROM DUAL;
-- Result: 2026-08-04

-- dd/mm/yyyy
SELECT TO_CHAR(SYSDATE, 'DD/MM/YYYY') FROM DUAL;
-- Result: 04/08/2026

-- Oracle default style (DD-MON-YY)
SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY') FROM DUAL;
-- Result: 04-AUG-2026

-- Full date with time
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
-- Result: 2026-08-04 14:30:45

-- Day name and month name
SELECT TO_CHAR(SYSDATE, 'Day, Month DD, YYYY') FROM DUAL;
-- Result: Tuesday , August  04, 2026

Changing Oracle Default Date Format

-- Session level
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';

-- Check current setting
SELECT VALUE FROM V$NLS_PARAMETERS WHERE PARAMETER = 'NLS_DATE_FORMAT';
Oracle gotcha: Oracle's DATE type always includes a time component. When you insert DATE '2026-08-04', the time defaults to midnight (00:00:00). Use TRUNC(SYSDATE) to strip the time portion.

Try Our Free Online SQL Formatter

Format, beautify, and validate your SQL queries — including all date functions — with our free online SQL formatter. 15+ dialects supported.

Open SQL Formatter →

5. Convert String to Date Across Databases

Converting a string to a date — often searched as "sql cast string to date" — works differently in each database. Here is how each engine handles it.

MySQL STR_TO_DATE

-- Convert dd/mm/yyyy string to DATE
SELECT STR_TO_DATE('04/08/2026', '%d/%m/%Y');
-- Result: 2026-08-04 (DATE type)

-- Convert with time
SELECT STR_TO_DATE('04-08-2026 14:30:45', '%d-%m-%Y %H:%i:%s');

PostgreSQL TO_DATE and CAST

-- Using TO_DATE
SELECT TO_DATE('04/08/2026', 'DD/MM/YYYY');
-- Result: 2026-08-04

-- Using CAST (ISO format only)
SELECT CAST('2026-08-04' AS DATE);

-- Using :: operator
SELECT '2026-08-04'::DATE;

-- With timestamp
SELECT TO_TIMESTAMP('04/08/2026 14:30', 'DD/MM/YYYY HH24:MI');

SQL Server CAST and CONVERT

-- ISO format string (always works)
SELECT CAST('2026-08-04' AS DATE);

-- dd/mm/yyyy string with CONVERT style
SELECT CONVERT(DATE, '04/08/2026', 103);
-- Style 103 = dd/mm/yyyy

-- mm/dd/yyyy string
SELECT CONVERT(DATE, '08/04/2026', 101);
-- Style 101 = mm/dd/yyyy

-- TRY_CONVERT (returns NULL instead of error)
SELECT TRY_CONVERT(DATE, 'invalid-date', 103);
-- Result: NULL

Oracle TO_DATE

-- Convert string to date
SELECT TO_DATE('04/08/2026', 'DD/MM/YYYY') FROM DUAL;

-- With time
SELECT TO_DATE('2026-08-04 14:30:45', 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;

-- Handle invalid dates with DEFAULT ON CONVERSION ERROR (12cR2+)
SELECT TO_DATE('invalid' DEFAULT '2026-01-01' ON CONVERSION ERROR,
              'DD/MM/YYYY') FROM DUAL;

6. Date Format Best Practices

Always Store Dates as DATE Type

Never store dates as VARCHAR. It breaks indexing, prevents date arithmetic, and makes formatting inconsistent. Every database has a proper date type — use it.

Use ISO 8601 for Data Exchange

When passing dates between systems (APIs, CSV exports, ETL pipelines), use YYYY-MM-DD (ISO 8601). It is unambiguous — 2026-08-04 is always August 4th, regardless of locale.

Format in the Application Layer

For web applications, return dates as ISO strings from the database and format them to the user's locale in your frontend (JavaScript Intl.DateTimeFormat, Python strftime, etc.). This keeps your SQL portable and your UI flexible.

Be Explicit About Time Zones

Use TIMESTAMPTZ (PostgreSQL), DATETIMEOFFSET (SQL Server), or TIMESTAMP WITH TIME ZONE (Oracle) when timezone awareness matters. Avoid relying on the server's default timezone — it changes between environments.

Cross-Database Cheat Sheet

OperationMySQLPostgreSQLSQL ServerOracle
Format dateDATE_FORMAT(d, fmt)TO_CHAR(d, fmt)FORMAT(d, fmt)TO_CHAR(d, fmt)
String to dateSTR_TO_DATE(s, fmt)TO_DATE(s, fmt)CONVERT(DATE, s, style)TO_DATE(s, fmt)
Current dateCURDATE()CURRENT_DATEGETDATE()SYSDATE
Current datetimeNOW()NOW()GETDATE()SYSDATE
Extract yearYEAR(d)EXTRACT(YEAR FROM d)YEAR(d)EXTRACT(YEAR FROM d)
Date difference (days)DATEDIFF(d1, d2)d1 - d2DATEDIFF(DAY, d2, d1)d1 - d2

FAQ

How do I format a date as dd/mm/yyyy in SQL?

Each database has its own function. In MySQL use DATE_FORMAT(date_col, '%d/%m/%Y'). In PostgreSQL use TO_CHAR(date_col, 'DD/MM/YYYY'). In SQL Server use FORMAT(date_col, 'dd/MM/yyyy') or CONVERT(varchar, date_col, 103). In Oracle use TO_CHAR(date_col, 'DD/MM/YYYY').

How do I convert a string to a date in SQL?

Use CAST('2026-08-04' AS DATE) for ISO format strings. For custom formats: MySQL STR_TO_DATE(), PostgreSQL TO_DATE(), SQL Server CONVERT(DATE, string, style_code), Oracle TO_DATE(). Always validate the format mask matches your input string.

What is the SQL datetime format function in MySQL?

MySQL uses DATE_FORMAT(date_col, format_string) for both dates and datetimes. Time specifiers like %H (24-hour), %i (minutes), and %s (seconds) work when the column is DATETIME or TIMESTAMP. For DATE columns, time specifiers return zero values.

How to format a date in Oracle SQL?

Oracle uses TO_CHAR(date_col, 'format_mask'). Common masks: 'YYYY-MM-DD' for ISO, 'DD-MON-YYYY' for Oracle's default, 'DD/MM/YYYY' for European. You can change the session default display format with ALTER SESSION SET NLS_DATE_FORMAT.

How to get the current date in different SQL date formats?

MySQL: SELECT CURDATE() for date, NOW() for datetime. PostgreSQL: SELECT CURRENT_DATE for date, NOW() for timestamp. SQL Server: SELECT GETDATE() for datetime, CAST(GETDATE() AS DATE) for date only. Oracle: SELECT SYSDATE FROM DUAL for datetime, TRUNC(SYSDATE) for date only.