01 / 21

Date/Timestamp
Parsing in SQL

Learn how to parse, format, and convert dates and timestamps across different SQL dialects: the essential skill for any data query.

SQL

What Is a Date vs. a Timestamp?

DATE
Stores only the calendar date: year, month, day. Example: 2025-07-11
TIMESTAMP
Stores date plus time of day: hours, minutes, seconds. Example: 2025-07-11 14:30:00

Why Parsing Matters

Dates arrive in many formats: 'July 11, 2025', '11-07-2025', '2025/07/11', or even '07112025'. Without proper parsing, your queries will produce errors, wrong results, or silent data corruption.

1
Filtering: WHERE clauses comparing dates need real date types, not strings.
2
Sorting: String sorting puts '12-Jan' before '2-Feb' incorrectly.
3
Calculations: Finding the difference between two dates requires proper date types.

Anatomy of a Format String

Format String YYYY - MM - DD HH : MI Year (4 digit) Month (01-12) Day (01-31) Hour (00-23) Minute (00-59) Actual Value 2025 - 07 - 11 14 : 30

The Core Rule: Dialect Matters

There is no universal date-parsing function in SQL. Every database engine uses a different function name and different format codes. You must know which dialect you are writing for.

Parse a Date String MySQL STR_TO_DATE() '%Y-%m-%d' PostgreSQL TO_DATE() 'YYYY-MM-DD' SQL Server CONVERT() style codes (e.g., 23) Oracle TO_DATE('...','YYYY-MM-DD') SQLite date() / strftime()

Format Codes Across Dialects

Component MySQL PostgreSQL / Oracle SQL Server
4-digit Year %Y YYYY yyyy
2-digit Month %m MM MM
2-digit Day %d DD dd
Hour (24h) %H HH24 HH
Minute %i MI mm
Second %s SS ss

Parsing vs. Formatting

Parsing (String → Date)
Convert text into a date type the database can use for math and filtering.

MySQL: STR_TO_DATE('07/11/2025', '%m/%d/%Y')
PostgreSQL: TO_DATE('07/11/2025', 'MM/DD/YYYY')
Formatting (Date → String)
Convert a date type into a readable text for display.

MySQL: DATE_FORMAT(my_date, '%M %d, %Y')
PostgreSQL: TO_CHAR(my_date, 'Month DD, YYYY')

The CAST Shortcut

Most SQL dialects support CAST (or its shorthand ::) for simple, ISO-format conversions. This works when your string is already in a standard format.

-- Standard SQL CAST SELECT CAST('2025-07-11' AS DATE); -- PostgreSQL shorthand SELECT '2025-07-11'::DATE; -- SQL Server TRY_CAST (safe version) SELECT TRY_CAST('2025-07-11' AS DATE);

Step-by-Step Approach

1
Identify the input format. Look at the raw string. Is it '2025-07-11', '07/11/2025', or 'July 11, 2025'?
2
Know your dialect. Determine whether you're writing MySQL, PostgreSQL, SQL Server, Oracle, or SQLite.
3
Build the format string. Map each part of the input to the correct format code for your dialect.
4
Call the parsing function. Use STR_TO_DATE, TO_DATE, CONVERT, or CAST as appropriate.
5
Test with edge cases. Try NULL values, invalid dates (Feb 30), and different locales.

Example 1: MySQL Parsing

Task: A column stores dates as text in the format '07/11/2025'. Convert it to a proper DATE for filtering.

SELECT order_id, STR_TO_DATE(order_date_text, '%m/%d/%Y') AS order_date FROM orders WHERE STR_TO_DATE(order_date_text, '%m/%d/%Y') >= '2025-01-01';

Example 2: PostgreSQL Timestamps

Task: Parse a timestamp string '11-Jul-2025 14:30' into a TIMESTAMP type.

SELECT TO_TIMESTAMP( '11-Jul-2025 14:30', 'DD-Mon-YYYY HH24:MI' ) AS event_time;

Example 3: SQL Server CONVERT

Task: Convert the string '2025-07-11T14:30:00' to a DATETIME and also display it in US format.

-- Parsing: string to datetime SELECT CONVERT(DATETIME, '2025-07-11T14:30:00', 126); -- Formatting: datetime to US-style string SELECT CONVERT(VARCHAR, GETDATE(), 101); -- Result: '07/11/2025'

Extracting Date Parts

Once you have a proper date, you can extract individual parts like the year, month, or day of the week.

Goal MySQL PostgreSQL SQL Server
Get Year YEAR(d) EXTRACT(YEAR FROM d) YEAR(d)
Get Month MONTH(d) EXTRACT(MONTH FROM d) MONTH(d)
Get Day of Week DAYOFWEEK(d) EXTRACT(DOW FROM d) DATEPART(dw, d)

Date Arithmetic

After parsing, you can add or subtract intervals and compute differences between dates.

-- MySQL: add 30 days SELECT DATE_ADD(order_date, INTERVAL 30 DAY); -- PostgreSQL: add 30 days SELECT order_date + INTERVAL '30 days'; -- SQL Server: add 30 days SELECT DATEADD(day, 30, order_date); -- Difference in days (PostgreSQL) SELECT end_date - start_date;

Common Mistakes

✕ MM vs DD Swap
Confusing month and day: is '07/11' July 11th or November 7th? Always verify the source format before writing your format string.
✕ Wrong Dialect Codes
Using MySQL's %m in PostgreSQL (which expects MM) or using Oracle's format codes in SQL Server. Each dialect has its own syntax.
✕ Comparing Strings
Using WHERE date_col > '2025-01-01' without parsing. String comparison sorts lexicographically, which may silently give wrong results.

Summary: Quick Reference

Task MySQL PostgreSQL SQL Server
String → Date STR_TO_DATE() TO_DATE() CONVERT / TRY_CAST
String → Timestamp STR_TO_DATE() TO_TIMESTAMP() CONVERT(DATETIME,...)
Date → String DATE_FORMAT() TO_CHAR() CONVERT(VARCHAR,...)
Extract Part YEAR() / MONTH() EXTRACT(...) DATEPART()
Add Interval DATE_ADD() + INTERVAL DATEADD()

Question 1 of 5

In MySQL, which function converts a text string into a DATE type?

Question 2 of 5

Which MySQL format string correctly parses the text '25-12-2024' (day-month-year)?

Question 3 of 5

In PostgreSQL, what does the format code HH24 represent?

Question 4 of 5

What is the main difference between parsing and formatting a date in SQL?

Question 5 of 5

A SQL Server developer needs to convert the ISO 8601 string '2025-07-11T14:30:00' to DATETIME. Which approach is correct?