日期:2014-05-18  浏览次数:20515 次

在sqlserver数据库中,如何得到当前日期任意天前的日期?
例如今天是2007-9-13,
我要得到一天前,两天前   ,一个星期前,一个月前。。。。。
的日期,
这个是在做查询是要用到的,麻烦高手指点一下!
小弟不胜感激!

------解决方案--------------------
declare @ndays int
set @ndays=3
select dateadd(day,-@ndays,getdate())

-----------------------
2007-09-10 14:23:18.830

(1 row(s) affected)
------解决方案--------------------
dataadd()函数
------解决方案--------------------
declare @ndays int,
@nweeks int,
@nmonths int
select @ndays=3,@nweeks=3,@nmonths=3
select dateadd(day,-@ndays,getdate()) as SomedaysAgo
,dateadd(wk,-@nweeks,getdate()) as SomeweekssAgo
,dateadd(m,-@nmonths,getdate()) as SomemothsAgo
/******
SomedaysAgo SomeweekssAgo SomemothsAgo
----------------------- ----------------------- -----------------------
2007-09-10 14:26:30.690 2007-08-23 14:26:30.690 2007-06-13 14:26:30.690
**********/
------解决方案--------------------
DATEADD (datepart , number, date )