Search code examples
datastage

How to subtract dates using OCONV


Background: I am responsible for creating database extracts using datastage. We have over five years of data with a CreatedDt (date datatype). In the export query, I need to add a BETWEEN clause for CreatedDt. Because the datastage job is generalized and parameterized, I need to have the dates calculated in the Sequence.

For example: Select * from tableA where CreatedDt BETWEEN <<start Date>> and <<End Date>>

For one of those queries, I need to go back four months and get 3 months of data (skipping the previous month of now). For example:

Assuming today's date: 02/22/2019

I need:

Start Date: 10/01/2018
End Date: 12/31/2018

(basically skip on month back and go three months)

I know this means the following calculations are being used:

  • Subtract from "Today" (twice)
  • Go to Beginning of the Month (for the start date)
  • Go to Last Day of the Month (for the end date)

I'm working with this formula:

OConv(Iconv(Date(),"D/YMD[4,2,2]") + 1), "D/YMD[4,2,2]")

From what I understand the '+1' represents adding/subtracting by day. using the previously mentioned calculation issue.

I tried:

OConv(Iconv(Date(),"D/YM[4,2]") + 1), "D/YMD[4,2,2]")

Thinking that I was +1 a month, But this ended up years away from my target point.

I need to know how to do the operations I need on a given date. Or if there's another way, I'm more than open.

Thanks


Solution

  • Use the Div and Mod functions.

    Example for 13 months ago:

    OConv(Date(),'D-Y[4]')-Div(13,12):'-':OConv(Date(),'D-M[2]')-Mod(13,12):'-':oconv(Date(),'D-D[2]')