Add or Subtract Weeks to a Date – Apache Impala

There are plenty of built-in functions available in Apache Impala that helps in analyzing the data and business logic implementations. It relieves a developer’s worries and eliminates the necessity for user-defined functions. Built-in functions aid analysts in the process of analyzing, cleansing, transforming, and modeling data in order to identify valuable information, draw conclusions, and support decision-making.

This is one of a series of articles describing how to use “date-time” functions. We’ll go through how to add and subtract a certain number of weeks from a given date, in this post.

WEEKS_ADD and WEEKS_SUB are the functions that adds and subtracts a specified number of weeks from a TIMESTAMP value. These functions deal with an actual TIMESTAMP value instead of a string that is converted to a TIMESTAMP.

Syntax:
WEEKS_ADD(TIMESTAMP startdate, INT days),
WEEKS_ADD(TIMESTAMP startdate, BIGINT days)
WEEKS_SUB(TIMESTAMP startdate, INT days),
WEEKS_SUB(TIMESTAMP startdate, BIGINT days)

Examples:

SELECT NOW() AS CurrentDate, WEEKS_ADD(NOW(), 2) DateAfter2Weeks;
….—…………CurrentDate…………….|………..–.DateAfter2Weeks–………..
2021-10-30 09:15:41.895351000 | 2021-11-13 09:15:41.895351000

SELECT NOW() AS CurrentDate, WEEKS_SUB(NOW(), 2) DateBefore2Weeks;
….—…………CurrentDate…………….|………..–.DateBefore2Weeks–………..
2021-10-30 09:15:41.895351000 | 2021-10-16 09:15:41.895351000

I hope you found this post to be informative.

Please join our mailing list to receive more interesting information.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s