CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK-3-5-QUESTIONS practice question 22 of 25

21 of 55. What is the behavior of the function date_sub(start, days) if a negative value is passed into the days parameter?

Full set: 135 questions
Answer options
A: The number of days specified will be added to the start date. Correct answer
B: An error message of an invalid parameter will be returned.
C: The same start date will be returned.
D: The number of days specified will be removed from the start date.
Correct Answer:
A. The number of days specified will be added to the start date.
Explanation

The datesub(start, days) function subtracts the integer value specified in the days parameter from the start date. According to standard mathematical principles, subtracting a negative number is equivalent to adding its positive counterpart. For example, datesub(date, -10) is processed as date - (-10), which resolves to date + 10. Therefore, passing a negative value for the days parameter results in the specified number of days being added to the start date.

Why Incorrect

B. The function is designed to accept any integer for the days parameter, including negative values, and will not return an error for a valid negative integer.
C. The start date is returned unmodified only if the days parameter is zero, not when it is a non-zero negative number.
D. This describes the behavior when a positive value is passed to the days parameter, which is the opposite of the scenario described in the question. ---

References

1. Apache Spark 3.4.1 Documentation

"Built-in Functions": The documentation for datesub(startDate

numDays) states it "Returns the date that is numDays before startDate." Logically

a date that is "-10" days before a start date is 10 days after it. This is consistent with the behavior of its counterpart

dateadd.

Source: Apache Spark Official Documentation. Navigate to SQL Reference -> Built-in Functions -> Datetime Functions.

2. Databricks SQL Language Reference

"datesub function": The documentation specifies the function's purpose is to return the date that is days before startdate. It also notes that datesub(startdate

days) is a synonym for startdate - days. This confirms that standard interval arithmetic applies

where subtracting a negative value results in addition.

Source: Databricks SQL Language Reference. Search for the "datesub function".

Scroll to Top