我有一个合同管理系统,数据库有这样的资料:合同签定时间,比如2001-1-1(datetime类型),合同年限比如4年(varchar类型),假如我要查询到今天到期的合同,怎么查询啊啊

解决方案 »

  1.   

    select * from table where DATEADD ( year, cast(合同年限 as int ), 合同签定时间)>getdate()
      

  2.   

    select * from table where dateadd(yy,4,时间字段)>=getdate()
      

  3.   

    最好是将合同年限设为int型比较好做一些,可以这样查
    select * from table where convert(char(10),dateadd(year,cast(htnx as int(4)),htqdrq),20) = '2004-11-22'
    htnx:合同年限
    htqdrq:合同签定日期
      

  4.   

    select * from table where dateadd(yy,4,时间)>=getdate()
      

  5.   

    字符型也可以比较
    select * from table
    where  DATEDIFF(year,合同签定时间,getdate()) > '4'
      

  6.   

    select * from table where year(getdate())-year(签定日期)=合同期限 And Month(getdate())=month(签定日期) And day(getdate())=day(签定日期)