Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL where Date from today minus and plus days

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.

like image 754
Captain16 Avatar asked May 12 '13 04:05

Captain16


People also ask

How do I query today's date in SQL?

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 .

How do I subtract two dates in SQL?

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.


2 Answers

try,

WHERE CUS_joinDate BETWEEN DATEADD(mm,-1,GETDATE()) AND DATEADD(mm,1,GETDATE())
  • SQLFiddle Demo
like image 182
John Woo Avatar answered Oct 06 '22 23:10

John Woo


You can use CURDATE() and DATEADD()

W3SCHOOLS SQL DATES

like image 26
Ben C Wang Avatar answered Oct 07 '22 00:10

Ben C Wang