Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BETWEEN two dates with functions SQL

I would like to extract data from the 20th last month until now, but is it impossible to make functions when making a BETWEEN AND command?

WHERE ([dtUpdated] BETWEEN ((Year(Date()))-(Month(Date())-1)-20)
    AND (Date()))
like image 695
André Avatar asked Sep 11 '26 11:09

André


1 Answers

You can use DateAdd to subtract one month from today's date. Here's an example from the Immediate window.

? Date()
8/15/2013 
? DateAdd("m", -1, Date())
7/15/2013 

Then you can determine the Year and Month of that previous date.

? Year(DateAdd("m", -1, Date()))
 2013 
? Month(DateAdd("m", -1, Date()))
 7 

So finally you can give DateSerial the Year, Month, and 20 as the day.

? DateSerial(Year(DateAdd("m", -1, Date())), _
    Month(DateAdd("m", -1, Date())), 20)
7/20/2013 

In a query, try it like this ...

WHERE [dtUpdated] BETWEEN
    DateSerial(
        Year(DateAdd("m", -1, Date())),
        Month(DateAdd("m", -1, Date())),
        20)
    AND Date()
like image 103
HansUp Avatar answered Sep 12 '26 23:09

HansUp