Data Engineering Spark SQL - Spark SQL Functions - Date Manipulation Functions

Date Manipulation Functions


This article discusses essential date manipulation functions in Spark SQL, including getting current dates and timestamps, date arithmetic, truncating and date_trunc functions, extracting information using date_format, working with calendar functions, and dealing with Unix timestamps.

Explanation for the video

The video embedded in this article complements the text by providing visual demonstrations and explanations of the concepts discussed. It is recommended to watch the video alongside reading the text for a better understanding.

Key Concepts Explanation

Getting Current Date and Timestamp

The current_date function returns today’s date, while current_timestamp provides the current timestamp. These functions do not require () and follow specific formats.

SELECT current_date AS current_date
SELECT current_timestamp AS current_timestamp

Date Arithmetic

Functions like date_add, date_sub, datediff, and add_months help perform date calculations. For instance:

SELECT date_add(current_date, 32) AS result
SELECT datediff('2019-03-30', '2017-12-31') AS result

Beginning Date or Time - trunc and date_trunc

Using trunc and date_trunc, you can get the starting date or time for a given period, like the beginning of a month or year. Note that date_trunc can handle timestamps with more precision.

SELECT trunc(current_date, 'MM') AS beginning_date_month
SELECT date_trunc('HOUR', current_timestamp) AS hour_beginning

Extracting information using date_format

The date_format function extracts date/time information, such as year, month, day, and time components, from timestamps or dates.

SELECT date_format(current_timestamp, 'yyyy') AS year
SELECT date_format(current_timestamp, 'MM') AS month

Extracting information - Calendar functions

Functions like day, month, weekofyear, etc., provide individual date components from dates or timestamps for specific calculations.

SELECT year(current_date) AS year
SELECT month(current_date) AS month

Dealing with Unix Timestamp

Functions like from_unixtime and to_unix_timestamp help in converting between Unix timestamps and regular timestamps. For instance:

SELECT from_unixtime(1556662731) AS timestamp
SELECT to_unix_timestamp('2019-04-30 18:18:51') AS unixtime

Hands-On Tasks

Explore the date manipulation functions mentioned in the article. Conduct the following tasks:

  1. Use current_date and current_timestamp functions to get today’s date and current time.
  2. Utilize date_add and datediff to perform arithmetic on dates.
  3. Experiment with trunc and date_trunc to find the beginning date or time.
  4. Extract date information using date_format for different formats.
  5. Use calendar functions like year, month, weekofyear, etc., to extract date components.
  6. Try converting between regular timestamps and Unix timestamps using from_unixtime and to_unix_timestamp.


In summary, this article delves into the essential date manipulation functions in Spark SQL, covering a wide range of operations from getting current dates and times to dealing with Unix timestamps. Practice and experiment with these functions to enhance your understanding and proficiency in date calculations.

Invitation to Join the Community

For further learning, engagement, or queries, feel free to join our community. Sign up or log in to participate in discussions and seek more guidance on Spark SQL and other related topics.

Watch the video tutorial here