我有这样的代码:
select CUS_Id, CUS_Name, CUS_JoinDate
from CUSTOMER
where CUS_joinDate between '04-12-2013' and '06-12-2013'
我怎样才能使它“在哪里” CUS_JoinDate 将被声明为从今天日期(减去 1 个月和今天 + 1 个月)?
所以每次我运行报告时,它都取决于当前日期,它会自动减去和加上 30 天。
我有这样的代码:
select CUS_Id, CUS_Name, CUS_JoinDate
from CUSTOMER
where CUS_joinDate between '04-12-2013' and '06-12-2013'
我怎样才能使它“在哪里” CUS_JoinDate 将被声明为从今天日期(减去 1 个月和今天 + 1 个月)?
所以每次我运行报告时,它都取决于当前日期,它会自动减去和加上 30 天。
您可以使用 CURDATE() 和 DATEADD()
试试这个:
select CUS_Id, CUS_Name, CUS_JoinDate
from CUSTOMER
where CUS_joinDate BETWEEN DATEADD(DAY,-1,GETDATE()) AND DATEADD(DAY,1,GETDATE())
但是,如果您需要添加月/年日期,则需要使用 DATEADD() 函数。
它可以用作:
SELECT GETDATE(), 'Today'
UNION ALL
SELECT DATEADD(DAY, 10, GETDATE()), '10 Days Later'
UNION ALL
SELECT DATEADD(DAY, –10, GETDATE()), '10 Days Earlier'
UNION ALL
SELECT DATEADD(MONTH, 1, GETDATE()), 'Next Month'
UNION ALL
SELECT DATEADD(MONTH, –1, GETDATE()), 'Previous Month'
UNION ALL
SELECT DATEADD(YEAR, 1, GETDATE()), 'Next Year'
UNION ALL
SELECT DATEADD(YEAR, –1, GETDATE()), 'Previous Year'
结果集:
2011-05-20 21:11:42.390 Today
2011-05-30 21:11:42.390 10 Days Later
2011-05-10 21:11:42.390 10 Days Earlier
2011-06-20 21:11:42.390 Next Month
2011-04-20 21:11:42.390 Previous Month
2012-05-20 21:11:42.390 Next Year
2010-05-20 21:11:42.390 Previous Year