I am trying to grab orders between certain dates using the BETWEEN operator in SQL.
My date formats are in dd/mm/yyyy format.
ord_fdate is the from date: Currently 01/03/2012
ord_tdate is the to date: Currently 07/03/2012
I thought this would return all orders from 01/03/2012 (including 01/03/2012) to 07/03/2012 (including 07/03/2012).
However, it does not get orders with dates 07/03/2012
My query below; i have also included the debug output of the query;
<cfset ord_fdate = DateFormat(ord_fdate, "dd/mm/yyyy")>
<cfset ord_tdate = DateFormat(ord_tdate, "dd/mm/yyyy")>
<cfquery name="getOrders" datasource="#application.dsn#">
SELECT
dbo.tbl_orders.uid_orders,
dbo.tbl_orders.dte_order_stamp
FROM
dbo.tbl_orders
WHERE dbo.tbl_orders.uid_order_webid=<cfqueryparam cfsqltype="cf_sql_integer" value="#session.webid#">
AND bit_order_archive=<cfqueryparam cfsqltype="cf_sql_bit" value="no">
AND txt_order_status=<cfqueryparam cfsqltype="cf_sql_varchar" value="Awaiting Dispatch">
AND dte_order_stamp BETWEEN <cfqueryparam cfsqltype="cf_sql_date" value="#createODBCDate(ord_fdate)#"> AND <cfqueryparam cfsqltype="cf_sql_date" value="#createODBCDate(ord_tdate)#">
ORDER BY dte_order_stamp DESC
</cfquery>
Debug;
WHERE (dbo.tbl_orders.uid_order_webid=?
AND bit_order_archive=?
AND txt_order_status=?
AND dte_order_stamp BETWEEN ? AND ?)
ORDER BY dte_order_stamp DESC
Query Parameter Value(s) -
Parameter #1(cf_sql_integer) = 1
Parameter #2(cf_sql_bit) = NO
Parameter #3(cf_sql_varchar) = Awaiting Dispatch
Parameter #4(cf_sql_date) = {ts '2012-03-01 00:00:00'}
Parameter #5(cf_sql_date) = {ts '2012-03-07 00:00:00'}
Not sure why it is not working.
Database is SQL 2008
Any ideas?
Does your date field include time? If so, between will only give you values >= the first date, and <= the second date at midnight. Any date that includes a time past midnight will be ignored.
I usually do something like this when dealing with dates:
If I want everything from 1/1/2012 I set up the start date as 1/1/2012, and the end date as one day later (at midnight)…
Then select the values like this:
When checking against the end date I want anything with a value less than the end date. This will include values such as 1/1/2012 12:30, 1/1/2012 23:20, etc.