Sandesh Gitte
Sandesh Gitte

Reputation: 11

Teradata - Calculate the previous quarter date start date and end date from current date

I have current_date in Teradata which 18 DEC 2019

I have to calculate the previous quarter start date and end date from the above current_date.

Input               =     '2019-12-18'
Output Start Date   =     '2019-07-01' 
Output End Date     =     '2019-09-30'

Upvotes: 0

Views: 4038

Answers (1)

ravioli
ravioli

Reputation: 3833

You should be able to do this using the TRUNC function, something like:

SELECT 
  TRUNC(ADD_MONTHS(CURRENT_DATE, -3), 'Q') AS Start_Quarter, -- Previous quarter start
  TRUNC(CURRENT_DATE, 'Q') - 1 AS End_Quarter -- Current quarter start date - 1 day

Give it a try and let me know. This assumes the mistake in the manual is still considered a "mistake".

Also, depending on what TD version you're using, you may be able to use built-in functions:

SELECT 
  TD_QUARTER_BEGIN(CURRENT_DATE) AS Start_Quarter,
  TD_QUARTER_END(CURRENT_DATE) AS End_Quarter

Reference
TD Manual
Built-in functions

Upvotes: 2

Related Questions