to_number(to_char(sysdate,'yyyy')) - to_number(to_char(day1,'yyyy')) as yeardel ,
to_number(to_char(sysdate, 'mm'))   -   to_number(to_char(day1, 'mm')) as mothdel,
to_number(to_char(sysdate, 'dd'))   -   to_number(to_char(day1, 'dd')) as daydel 这样是年月日,分开来相减的,这样减出来后,月和日 可能有负数,所以还得判断。假如day1的值为2008-12-3 则结果是 年3,月-1,日4  而不是 年2,月11,日4  
 所以想用其他的办法.
trunc((months_between(trunc(sysdate,'dd'), day1))/12) as year  这样,结果是单纯的年,并且是正确的年.我想用这种类似的函数计算出月和日.   可是不知道怎么使用函数??

解决方案 »

  1.   

    select datediff(day,'2006-10-1','2007-10-1'),你这样看,
      

  2.   

    select datediff(year,'2006-10-1','2007-10-1')
      

  3.   

    select datediff(month,'2006-10-1','2007-10-1')赋值没关系啊,取绝对值好了
      

  4.   

    http://zlk.iteye.com/blog/642442  计算两个时间差
      

  5.   

    select   datediff(day,'2004-01-01',getdate())    相差天数   
    select   datediff(month,'2004-01-01',getdate())       相差月数
    select   datediff(year,'2004-01-01',getdate())       相差年数
    select   datediff(week,'2004-01-01',getdate())       相差星期
    select   datediff(Quarter,'2004-01-01',getdate())    相差季度
      

  6.   

    http://www.cnblogs.com/wulg/archive/2011/03/17/1986994.htmlC# 根据年、月、周、星期获得日期等  照着改吧下就可以了
      

  7.   

    年数差:
    select ABS(to_number(to_char(to_date('2009-12-12','yyyy-mm-dd'),'yyyy'))  - to_number(to_char(to_date('2011-12-12','yyyy-mm-dd'), 'yyyy')))  from dual;月数:
    with temp as(
    select to_date('2006-05-01','yyyy-mm-dd') time2,to_date('2009-06-08','yyyy-mm-dd') time1 from dual
    union all
    select to_date('2008-12-01','yyyy-mm-dd') time2,to_date('2010-02-10','yyyy-mm-dd') time1 from dual
    )
    select case when to_number(to_char(time1,'MM')) - to_number(to_char(time2,'MM')) >= 0 
    then to_number(to_char(time1,'MM')) - to_number(to_char(time2,'MM'))
    else to_number(to_char(time1,'MM'))+12 - to_number(to_char(time2,'MM')) + 1
    end 相差月数
     from temp
    天数差:
    select     floor(to_date( '20110409 ', 'yyyymmdd ')     -     to_date( '20110408 ', 'yyyymmdd '))     from     dual;