Databricks-Spark-Assoc Using Spark SQL Practice Question
An analyst has a Spark SQL DataFrame named events with a string column event_time in the format 'yyyy-MM-dd HH:mm:ss'. They want to add a new column event_date containing only the date portion, keeping the original column intact. Which expression should they use in a select statement?
⚠ Common exam trap
The trap here is choosing substring because it visually looks like it extracts the date, while ignoring that it returns a string and is not robust to format variations.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
to_date(event_time, 'yyyy-MM-dd HH:mm:ss')
Using to_date with the matching pattern correctly parses the timestamp string and returns a date-typed column containing only the date component. The other functions either return strings, operate on date types rather than strings, or rely on fragile character extraction, so they do not produce a proper date column as required.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
date_format(event_time, 'yyyy-MM-dd')
Why it's wrong here
date_format converts a date or timestamp into a formatted string, not a date type. If event_time is already a string, date_format may fail or require an implicit cast. It also returns a string rather than a date, so the resulting column would not be a true date column, contrary to the requirement of extracting the date portion.
- ✗
trunc(event_time, 'MM')
Why it's wrong here
The trunc function truncates a date or timestamp to the specified unit, such as month or year. It does not parse a string with a custom pattern and cannot extract just the date portion from 'yyyy-MM-dd HH:mm:ss'. Applying it to a string column would either error or produce unintended results, so it does not meet the scenario's need.
- ✗
substring(event_time, 1, 10)
Why it's wrong here
While substring would extract the first ten characters and coincidentally yield the date text for this format, it returns a string rather than a date. This approach is brittle: it fails for other valid timestamp formats and does not validate the date. It also does not leverage Spark SQL's date parsing, so it is not the appropriate expression here.
- ✓
to_date(event_time, 'yyyy-MM-dd HH:mm:ss')
Why this is correct
The to_date function parses the string using the supplied pattern and returns a date value containing only the date portion. Using the format string ensures correct parsing of the timestamp text. This produces the desired event_date column while leaving the original event_time column unchanged, which is exactly what the scenario requires.
About these practice questions
This Databricks-Spark-Assoc question is part of Courseiva's 295-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Databricks exam blueprint
This Databricks-Spark-Assoc practice question is part of Courseiva's free Databricks certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the Databricks-Spark-Assoc exam.