months_between terradata
Teradata Teradata Discussion Forums Teradata.com Discussion Forum
Visit Teradata.com
Home       Guidelines    Member List
Welcome Guest ( Login | Register )
        


This online forum is for user-to-user discussions of Teradata products, and is not an official customer support channel for Teradata. If you require direct assistance, please contact Teradata support.


months_between terradata Expand / Collapse
Author
Message
Posted 9/17/2007 5:12:36 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 9/17/2007 5:07:00 AM
Posts: 1, Visits: 1
I need to calculate the exact months number between two dates

e.g.

as same as ORACLE

select months_between (sysdate, sysdate - 14 ) from dual

Thank you


sylvi
Post #8907
Posted 9/17/2007 6:52:27 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 11/12/2008 9:19:53 PM
Posts: 469, Visits: 463
You can either join with the calendar tables or play around using the EXTRACT functions to build the logic to do the same in TD.

something on this line .....

SELECT DT1, DT2, ((EXTRACT(YEAR FROM DT1) - EXTRACT(YEAR FROM DT2))*12
+ (EXTRACT(MONTH FROM DT1) - EXTRACT(MONTH FROM DT2)) + (EXTRACT(DAY FROM DT1) - EXTRACT(DAY FROM DT2))/31.0000000) MONTHS_BETWEEN
FROM MYTABLE
;


SELECT DT1, DT2, ((C1.MONTH_OF_CALENDAR - C2.MONTH_OF_CALENDAR) + (C1.DAY_OF_MONTH - C2.DAY_OF_MONTH)/31.0000000) MONTHS_BETWEEN
FROM MYTABLE
INNER JOIN SYS_CALENDAR.CALENDAR C1
ON MYTBL.DT1 = C1.CALENDAR_DATE
INNER JOIN SYS_CALENDAR.CALENDAR C2
ON MYTBL.DT2 = C2.CALENDAR_DATE
;
Post #8908
Posted 9/18/2007 8:31:25 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 11/27/2008 9:25:22 AM
Posts: 43, Visits: 51
Sylvi,

I think you could spend some time studying the interval datatype, that it is great with date calculations, but unfortunately it is not much intuitive.

One example that might help:

select date - (date - 14) MONTH

This query will return the number of months between the actual date and a date 14 days before, which would be 0 (zero) months.

I hope this helps!
Post #8924
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 ( 0 guests, 0 members, 0 anonymous members )
No members currently viewing this topic.


All times are GMT -5:00, Time now is 2:42pm

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.063. 7 queries. Compression Disabled.