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.
What Is a Date vs. a Timestamp?
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.
Anatomy of a Format String
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.
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
MySQL: STR_TO_DATE('07/11/2025', '%m/%d/%Y')
PostgreSQL: TO_DATE('07/11/2025', 'MM/DD/YYYY')
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.
Step-by-Step Approach
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.
Example 2: PostgreSQL Timestamps
Task: Parse a timestamp string '11-Jul-2025 14:30' into a TIMESTAMP type.
Example 3: SQL Server CONVERT
Task: Convert the string '2025-07-11T14:30:00' to a DATETIME and also display it in US format.
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.
Common Mistakes
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?