Sterve
Sterve

Reputation: 51

SQL Between Dates Syntax

I have a query that needs to return records where the ShipDate is between a range of dates. The following query works:

$query = "SELECT ECCustomerOrder.ShipDate, 
ECCustomers.Customer, 
ECCustomerOrder.PO, 
ECCustomerOrder.TotalCases, 
ECCustomerOrder.order_number
FROM ECCustomerOrder INNER JOIN ECCustomers ON ECCustomerOrder.ID = ECCustomers.ID
WHERE ECCustomerOrder.ShipDate >= $todayMinus";

$todayMinus is a php variable set earlier in the script that takes the current date and subtracts 15 days. What I can't get to work is the following:

$query = "SELECT ECCustomerOrder.ShipDate, 
ECCustomers.Customer, 
ECCustomerOrder.PO, 
ECCustomerOrder.TotalCases, 
ECCustomerOrder.order_number
FROM ECCustomerOrder INNER JOIN ECCustomers ON ECCustomerOrder.ID = ECCustomers.ID
WHERE ECCustomerOrder.ShipDate BETWEEN $todayMinus AND $todayPlus";

What is the proper syntax for this SQL query to return the records between the ShipDates of $todayMinus and $todayPlus?

Upvotes: 0

Views: 5606

Answers (1)

Marc B
Marc B

Reputation: 360672

assuming you're using plain dates, you'll have to quote them.

e.g.

ShipDate BETWEEN 2013-01-01 AND 2013-02-19

is incorrect, because 2013-01-01 is a substraction, you're actually doing

ShipDate BETWEEN 2011 AND 1992

try

ShipDate BETWEEN '$todayMinus' AND '$todayPlus'

Upvotes: 3

Related Questions