Sei sulla pagina 1di 6

Register Sign In Help

Community

Tenure in years and months between


two dates
Search all content This board

> Product >


Teradata Forums Database
> Tenure in years and months between two dates

Options

Happy_Gilmore N/A 04-30-2008 07:06 AM

Tenure in years and months between two dates

Hi,

Can someone tell me how to extract the tenure between two dates as
years and months using SQL?

thanks HG

0 Kudos Reply
6 REPLIES All forum topics Previous Topic Next Topic

mohammadismail7 Fan 04-30-2008 04:20 PM

Re: Tenure in years and months between two dates

hope this helps

select year('2008-04-30') - YEAR('2000-01-01') as years ,


month('2008-04-30')-MONTH('2000-01-01') as months

Thanks

0 Kudos Reply

SN Enthusiast 04-30-2008 05:29 PM

Re: Tenure in years and months between two dates

you can try this,

sel
Cast((((('2009-02-01')(date))-(('2008-01-01')(date)))year(4)) As
integer)||'year(s)'||
Cast((((('2009-02-01')(date))-(('2008-01-01')(date)))month(4)) As
integer) mod 12||'month(s)'
0 Kudos Reply

maclark N/A 12-15-2011 08:40 AM

Re: Tenure in years and months between two dates

This doesn't seem to work correctly.


select Cast((((('2007-01-31')(date))-(('2005-07-01')(date))) year(4)) As
integer) as years;
This gives the result of 2 which is incorrect. There is actually only1 year
and 6 months between the two dates.
Anybody have a solution to determine the correct number of FULL
years between two dates? I know I can get the number of days and
divide by 365 but that doesn't work when leap years are invovled.
Thanks,
Mike

0 Kudos Reply
dnoeth Senior Apprentice 12-15-2011 10:28 AM

Re: Tenure in years and months between two dates

Hi Mike,
2 is only "incorrect" for a human being, but "correct" for Standard SQL
:-)

SELECT
DATE'20070131'ASdt1,
DATE'20050701'ASdt2,
((dt1(INT))(dt2(INT)))/10000ASage

Dieter

0 Kudos Reply

hssahoo Fan 12-19-2011 04:20 AM

Re: Tenure in years and months between two dates

SELECT date '2011-02-28' as dt1,

date '2000-07-29' as dt2,

(dt1 - dt2) as no_of_days,

(dt1 - dt2)/365 as no_of_yrs1,


cast((dt1 - dt2)/365.25 as integer) as no_of_yrs2,

extract (year from dt1) - extract (year from dt2)

- case when extract(month from dt1) lt extract(month


from dt2) then 1

else (case when extract(day from dt1) lt extract(day


from dt2) then 1 else 0 end) end as no_of_yrs3;

0 Kudos Reply

rluebke Enthusiast 12-20-2011 10:50 AM

Re: Tenure in years and months between two dates

Can I suggest you use the SYS_CALENDAR.CALENDAR for doing this?


Even better, queries using it are easy to understand and maintain. It is,
however, very Teradata-ish.
R

0 Kudos Reply
Teradata Links

Support & Forums

Downloads

Community Feedback

Teradata.com | Help | | | | | | | | Teradata Support


Copyright 2004-2015 Teradata Corporation. Your use of this Teradata website is governed by the
Privacy Policy and the Terms of Use, including your rights to materials on this website, the rights you
grant to your submissions to this website, and your responsibilities regarding your conduct on this
website.

The Privacy Policy and Terms of Use for this Teradata website changed eective September 8, 2016.

Potrebbero piacerti anche