Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Get data for yesterday and day before

Tags:

sql

I am running this query:

select member, customerinfo.customerid -- ...other irrelevant columns...
from customerinfo, addressinfo
    where customerinfo.customerid = addressinfo.customerid
    and MEMBER = (Date(GetDate()-1))
    and addressinfo.addresstype = 's'

I it is obviously giving me data if Member = yesterday.

My question is, how do I structure the query to give me data if Member = the last 2 days (yesterday and the day before)?

like image 239
JeffreyLazo Avatar asked Dec 04 '22 09:12

JeffreyLazo


2 Answers

MEMBER BETWEEN (GETDATE() -2) AND (GETDATE() -1)

In SQL Server you can also try:

MEMBER BETWEEN DATEADD(day, -2, GETDATE()) AND DATEADD(day, -1, GETDATE())
like image 89
Habib Avatar answered Jan 03 '23 18:01

Habib


Change your query in:

SELECT member, customerinfo.customerid, ContactName, Address1,
Address2, City, State, ZIP, Country from customerinfo, addressinfo
WHERE customerinfo.customerid = addressinfo.customerid
and MEMBER >= (Date(GetDate()-2)) AND MEMBER <= (Date(GetDate()-1))
and addressinfo.addresstype = 's'
like image 28
Joe Taras Avatar answered Jan 03 '23 17:01

Joe Taras