Skip to the page
Tecyfy

Databricks interview question · medium

A dataset loaded into a Databricks notebook seems to have incorrect date formatting when queried using SQL. How can Ronan leverage Databricks utilities or SQL functions within the notebook environment to efficiently correct the date format for analysis?

The short answerRonan parses the date strings with todate (or trytodate) and a pattern that matches how they are actually written, then stores the result as a real DATE column so every query uses the parsed value.

The full answer

Ronan parses the date strings with to_date (or try_to_date) and a pattern that matches how they are actually written, then stores the result as a real DATE column so every query uses the parsed value.

1. Find out what the strings look like

SELECT order_date_raw, count(*) FROM raw.orders GROUP BY ALL ORDER BY 2 DESC LIMIT 20;

Mixed formats (10/27/2026, 2026-10-27, 27.10.2026) are the usual cause of "wrong" dates, and so is guessing dd/MM when the source writes MM/dd.

2. Parse with the right pattern

SELECT order_date_raw,
       to_date(order_date_raw, 'MM/dd/yyyy') AS order_date
FROM raw.orders;

Patterns are case-sensitive: MM is month, mm is minutes; yyyy is the year. Use to_timestamp when the string includes a time.

3. Don't let bad rows fail the query

With ANSI mode on (the default on current Databricks), to_date raises an error on a string it cannot parse. try_to_date returns NULL instead, which also lets you handle several formats:

SELECT order_date_raw,
       coalesce(
         try_to_date(order_date_raw, 'MM/dd/yyyy'),
         try_to_date(order_date_raw, 'yyyy-MM-dd'),
         try_to_date(order_date_raw, 'dd.MM.yyyy')
       ) AS order_date
FROM raw.orders;

Rows that are still NULL are the ones to look at.

4. Format only for display

date_format(order_date, 'dd MMM yyyy') turns a date back into a string for a report. Keep dates as DATE for filtering, sorting and joining; formatting them as strings early is what causes many of these problems.

5. Fix it once, at the source of the analysis

Instead of parsing in every query, write a cleaned table or view with the parsed column:

CREATE OR REPLACE TABLE clean.orders AS
SELECT * EXCEPT (order_date_raw),
       coalesce(try_to_date(order_date_raw, 'MM/dd/yyyy'),
                try_to_date(order_date_raw, 'yyyy-MM-dd')) AS order_date
FROM raw.orders;

In a pipeline, the same expression goes in the silver step, with an expectation such as order_date IS NOT NULL so unparseable rows are counted or quarantined.

In PySpark the same functions exist: F.to_date("order_date_raw", "MM/dd/yyyy"), and F.try_to_date in recent versions.

Once dates are real DATE values, functions such as year(), date_trunc('MONTH', ...) and datediff() work as expected.

Why this matters in an interview: the question is less about one function than about where the fix belongs. Parsing in every analyst's query spreads the same assumption into many places, and when the source changes its format, each query breaks in a different way. Fixing the column once, testing it with a count of unparsed rows, and having everyone read the clean table keeps one definition of the date for the whole team.

Last updated 16 October 2026

Sources

Where each fact comes from

  1. 1Datetime patterns
  2. 2try_to_date function

Asked next