在SQL查询分析器里怎么比较时间

来源:百度知道 编辑:UC知道 时间:2024/06/12 13:02:31
表里有 执行时间 这个字段,是datetime类型的,我想找到执行时间距今三个月之内所以数据,查询分析器比较时间的条件怎么写。
执行时间的格式,例如: 2009-6-1
2009-11-12

1\如果是指90天
select *
from table
where datediff(day,time,getdate())<=90
或者
select *
from table
where time>=dateadd(day,-90,getdate())
2\如果是3月
select *
from table
where time>=dateadd(month,-3,getdate())
3\如果是当前月往前推两个自然月的1号算起
select *
from table
where time>=Dateadd(mm,datediff(mm,0,getdate())-2,0)

select *
from 表名 t
where to_char(t.执行时间,'yyyy-mm-dd') between to_char(add_months(sysdate,-3),'yyyy-MM-dd') and
to_char(sysdate,'yyyy-MM-dd')

select * from tb
where convert(varchar(10),dt,120)>=dateadd(mm,-3,convert(varchar(10),getdate(),120)

where 执行时间>'2009-6-1'
and 执行时间<'2009-11-12'