Convert Text to a Date in SQL
Converting text to a date parses strings such as 31/12/2026 into real dates, so you can sort, filter and group them by period.
Example query
This example assumes: raw_orders(id bigint, order_date_text text, amount_text text). It is written in PostgreSQL syntax, and your table and column names will differ from these.
SELECT id,
order_date_text,
TO_DATE(order_date_text, 'DD/MM/YYYY') AS order_date,
amount_text::numeric AS amount
FROM raw_orders
WHERE order_date_text ~ '^[0-9]{2}/[0-9]{2}/[0-9]{4}$'
AND amount_text ~ '^[0-9]+(\.[0-9]+)?$';How it works
- TO_DATE parses the string using the format you give it; DD/MM/YYYY says the day comes first.
- The regular-expression filters keep only rows that match the expected shape, so one bad value does not make the whole query fail.
- Casting amount_text to numeric turns the text into a number you can sum.
MySQL, BigQuery, SQL Server and SQLite differences
- MySQL: STR_TO_DATE(order_date_text, '%d/%m/%Y'). SQL Server: TRY_CONVERT(date, order_date_text, 103). BigQuery: PARSE_DATE('%d/%m/%Y', order_date_text), or SAFE.PARSE_DATE to return null instead of failing. SQLite stores dates as text and expects YYYY-MM-DD.
Common mistakes
- 01/02/2026 is 1 February in the UK and 2 January in the US; confirm the format with the data owner.
- A single malformed value raises an error in a plain cast; filter first or use a safe or try version where your database has one.
- Two-digit years and mixed formats in one column need to be handled separately.
Ask for it in plain English
Instead of writing the query yourself, you can type the question. SchemaChat turns it into SQL against your connected database or uploaded file, runs it, and shows the SQL and the figures it used so you can check the answer before you share it. Check that the query matches the definition your business uses.
- Convert the text order dates in the raw orders table to real dates and count orders per month.
- How many rows have a date that does not match the expected format?