Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Add function on Teradata? 2

Status
Not open for further replies.

DugzDMan

Programmer
Oct 19, 2000
157
US
Is there some type of DateAdd function on Teradata?

All I'm trying to do is select items from a table where a usage date is within the last 6 months.

Looking for something along these lines:

SELECT ITEM1, ITEMS2, ...
FROM MYDB.MYTABLE
WHERE USAGE_DATE > DATEADD(MONTH,-6,DATE)

Thx!
 
SELECT ITEM1, ITEMS2, ...
FROM MYDB.MYTABLE
WHERE USAGE_DATE > add_months(date,6)
 
Hi,
The appropriate ANSI SQL Syntax for your operation is

( DATE - INTERVAL ‘6’ MONTH )


However Teradata has an SQL EXTENSION only valid agaianst Teradata platforms called

ADD_MONTHS

as stated above. Here is what the the SQL manual Volume 5 Chapter 5 has to say about the difference between the two.




Normalization Behavior of ADD_MONTHS

The standard approach to interval month arithmetic is to increment MONTH and YEAR values as appropriate and retain the source value for DAY. This is a problem for the case when the target DAY value is smaller than the source DAY value from the source date.

For example, what approach should be taken to handle the result of adding one MONTH to a source DATE value of ‘1999-01-31’? Using the standard approach, the answer would be ‘1999-02-31’, but February 31 is not a valid date.

The behavior of ADD_MONTHS is equivalent to that of the ANSI SQL-99-compliant operations

DATE ± INTERVAL ‘n’ MONTH

and

TIMESTAMP ± INTERVAL ‘n’ MONTH

with one important difference.

The difference between these two scalar arithmetic operations is their behavior when a non-valid date value is returned by the function.

• ANSI SQL-99 arithmetic returns an error.

• ADD_MONTHS arithmetic makes normative adjustments and returns a valid date.



By the way all the SQL manuals ( and a lot of other manuals ) are available from


click on SQL reference and pick your release level.
 
"The difference between these two scalar arithmetic operations is their behavior when a non-valid date value is returned by the function.

• ANSI SQL-99 arithmetic returns an error."

Can anyone shed some light on it, why it was implemented this way? It's ANSI SQL, but is it neccessary to implement the flaws also?
So it's impossible to use INTERVAL YEAR or MONTH in date calculation.

And a second one:
Why the stupid maximum number of digits (4) from ANSI SQL was used?

select (current_date - date '1920-01-01') day(4);

Again this makes it unusable.

Btw, in Oracle it's 9.

Dieter
 
Thanks for all the replies. I'm am pulling data from Teradata V2R4.1.3 and the ADD_MONTHS function works perfectly. This will do exactly what I am needing to do. I've also bookmarked the doc page you sent tdatgod.

Thanks again for the answers!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top