I have codes like this:
select CUS_Id, CUS_Name, CUS_JoinDate
from CUSTOMER
where CUS_joinDate between '04-12-2013' and '06-12-2013'
How can I make it 'where' CUS_JoinDate will be declare as from Today Date(minus 1 month and today + 1 month)?
So every time I run the report it will depends on the current date and it will automatically minus and plus 30 days.
To get the current date and time in SQL Server, use the GETDATE() function. This function returns a datetime data type; in other words, it contains both the date and the time, e.g. 2019-08-20 10:22:34 .
To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year , quarter , month , day , minute , etc.
try,
WHERE CUS_joinDate BETWEEN DATEADD(mm,-1,GETDATE()) AND DATEADD(mm,1,GETDATE())
You can use CURDATE() and DATEADD()
W3SCHOOLS SQL DATES
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With