我有这样的代码:
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天。
答案 0 :(得分:15)
答案 1 :(得分:3)
您可以使用CURDATE()和DATEADD()
<强> W3SCHOOLS SQL DATES 强>
答案 2 :(得分:0)
试试这个:
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