--测试数据 create table a(id varchar(10), date1 datetime, date2 datetime) insert a select '01', '2001-01-01', '2001-01-06' union all select '02', '2004-05-09', '2009-05-15' union all select '03', '2005-01-06', '2005-01-07' union all select '03', '2005-01-06', '2005-01-07' --查询 select count(1) from a where datediff(dd, date1, date2)>2 --清除 drop table a
select count(1) from A where datediff(day,Date1,Date2)>2
select * from A where datediff(day,Date1,Date2)>2
select count(*) from A where datediff(day,Date1,Date2)>2
declare @a int ,@b int,@c int select @a=date1 from 表名 select @b=date2 from 表名 select @c=@b-@a if select * from 表名 where @c>2 print @c
select count(1) from A where datediff(day,Date1,Date2)>2 函数DATEDIFF 返回跨两个指定日期的日期和时间边界数。 语法 DATEDIFF ( datepart , startdate , enddate ) 改变参数datepart 可以改变返回的值,比如可以返回两个日期相隔的日期/年/时/分/秒 具体参数说明可以看SQL的帮助
create table a(id varchar(10), date1 datetime, date2 datetime)
insert a select '01', '2001-01-01', '2001-01-06'
union all select '02', '2004-05-09', '2009-05-15'
union all select '03', '2005-01-06', '2005-01-07'
union all select '03', '2005-01-06', '2005-01-07'
--查询
select count(1)
from a
where datediff(dd, date1, date2)>2
--清除
drop table a
where datediff(day,Date1,Date2)>2
select @a=date1 from 表名
select @b=date2 from 表名
select @c=@b-@a
if select * from 表名 where @c>2
print @c
函数DATEDIFF 返回跨两个指定日期的日期和时间边界数。
语法
DATEDIFF ( datepart , startdate , enddate )
改变参数datepart 可以改变返回的值,比如可以返回两个日期相隔的日期/年/时/分/秒
具体参数说明可以看SQL的帮助