TO_DATE()

SQL · Date Functions

Theory

TO_DATE converts text into a real DATE using a format mask that tells the database how to read the text. TO_DATE('11-07-2026', 'DD-MM-YYYY') says 'the first two digits are the day, then the month, then a four-digit year', so the result is 11 July 2026. Without the mask, '11-07-2026' is ambiguous: an American reader would see 7 November.

TO_DATE exists in Oracle, PostgreSQL and Snowflake, with mask codes like DD, MM, MON, YYYY. MySQL uses STR_TO_DATE('11-07-2026', '%d-%m-%Y'), SQL Server uses CONVERT(date, '11-07-2026', 105) or TRY_CONVERT, and SQLite's date() only understands ISO text (YYYY-MM-DD), so you rearrange the pieces yourself with substr(). Once a value is a real date, sorting, comparing and date arithmetic work correctly, whereas text dates in DD-MM-YYYY order sort wrongly ('02-01-2026' comes before '11-07-2025').

Why we use it

Data from CSV files, forms and old systems often arrives with dates as text in local formats. TO_DATE turns that text into proper dates so you can filter, sort and do date math reliably.

When to use it

Use it when loading or cleaning text dates that are not in ISO format. If the text is already ISO (2026-07-11), a simple CAST usually works. For messy data, prefer a 'try' version (TRY_TO_DATE, TRY_CONVERT) that returns NULL instead of failing.

How it works

  1. Text value arrives, e.g. '11-07-2026'
  2. Supply the mask that matches it: 'DD-MM-YYYY'
  3. Database reads day, month, year from positions
  4. Text matches the mask and is valid? Yes: Return DATE 2026-07-11. No: Error (NULL with TRY_).
  5. Sort, compare and do date math safely

Syntax

TO_DATE(string, 'format')

Example

SELECT TO_DATE('11-07-2026', 'DD-MM-YYYY');

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic