Invalid Operation On An ANSI DATETIME (Subtracting one timestamp from another in Teradata)

StackOverflow https://stackoverflow.com/questions/23524577

  •  17-07-2023
  •  | 
  •  

Question

I would like to create a WHERE condition to return results where only 1 day has passed between two timestamps. I tried this:

SELECT * FROM RDMAVWSANDBOX.VwNIMEventFct
INNER JOIN VwNIMUserDim ON VwNIMUserDim.NIM_USER_ID = VwNIMEventFct.NIM_USER_ID
INNER JOIN rdmatblsandbox.TmpNIMSalesForceDB ON TmpNIMSalesForceDB.EMAIL = VwNIMUserDim.USER_EMAIL_ADDRESS
WHERE (CONTRACT_EFFECTIVE_DATE - EVENT_TIMESTAMP) =1

But the result was an error message "Invalid Operation On An ANSI DATETIME value".

I guess that, looking at the code now, Teradata has no way of knowing whether the "1" in "= 1" is a day, hour or year.

How would I select data where only 1 day has passed between CONTRACT_EFFECTIVE_DATE and EVENT_TIMESTAMP?

Same again for 2 days, and 3 days etc?

Was it helpful?

Solution

If both columns are DATEs you can use =1which means one day.

For Timestamps you need to tell what kind of interval you want:

WHERE (CONTRACT_EFFECTIVE_DATE - EVENT_TIMESTAMP) DAY = INTERVAL '1' DAY

But i'm not shure if this is what you really want, what's your definition of 1 day?

Edit:

Based on your comment the best way should be:

WHERE CAST(CONTRACT_EFFECTIVE_DATE AS DATE) - CAST(EVENT_TIMESTAMP AS DATE) = 1

This avoids dealing with INTERVAL arithmetic :-)

OTHER TIPS

Not sure about Teradata, but I think most versions of SQL have built-in date math functions. In MSSQL for instance you could do this:

...
WHERE DATEDIFF(DAY, CONTRACT_EFFECTIVE_DATE, EVENT_TIMESTAMP) = 1

Or if you wanted to make sure 24 hours had passed you could do:

...
WHERE DATEDIFF(HOUR, CONTRACT_EFFECTIVE_DATE, EVENT_TIMESTAMP) = 1

Other SQL's have their own versions of this, and you may have to use 'D' or 'DD' instead of 'DAY' or something (and maybe 'HH' instead of 'HOUR' likewise).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top