0
Engaged
,
/t5/coldfusion-discussions/cfquery-comparing-same-dates/td-p/168285
Sep 24, 2008
Sep 24, 2008
Copy link to clipboard
Copied
I'm trying to query a range of dates from my database with
<cfquery>. I have two variables set to fromDate and toDate
which are input values from a FORM (user determines date range).
<cfset start_date = #CREATEODBCDATE(fromDate)#>
<cfset end_date = #CREATEODBCDATE(toDate)#>
My query looks like this:
<cfquery datasource="myds">
SELECT dbID, dbDate
FROM myTable
WHERE dbDate BETWEEN #start_date# AND #end_date#
</cfquery>
This works for every value except when the dbDate is the same as the end_date. There's a problem with TIME. dbDate in my database is of type datetime and holds both a date and time value (ex: 9-24-2008 9:03:58 AM). But the start_date and end_date do not have a time specified from the FORM, so they both display with 12:00:00 AM at the end. So lets say from my FORM, I choose 9-24-2008 for both my start and ending dates. In my query, it looks like...
dbDate BETWEEN 9-24-2008 12:00:00 AM AND 9-24-2008 12:00:00 AM
So if my dbDate's time is anything but 12:00:00 AM, it will not catch it in the comparison. I have to choose 9-25-2008 (9-25-2008 12:00:00 AM) to catch any dates on the 24th (and again, any dates in my database that are 9-25-2008 will now be discluded).
How can write this so the times of the dates are irrelevant? Is there a way to strip the time from dbDate? or to set the time of end_date so it looks like 9-24-2008 11:59:59 PM ? Any help is appreciated.
<cfset start_date = #CREATEODBCDATE(fromDate)#>
<cfset end_date = #CREATEODBCDATE(toDate)#>
My query looks like this:
<cfquery datasource="myds">
SELECT dbID, dbDate
FROM myTable
WHERE dbDate BETWEEN #start_date# AND #end_date#
</cfquery>
This works for every value except when the dbDate is the same as the end_date. There's a problem with TIME. dbDate in my database is of type datetime and holds both a date and time value (ex: 9-24-2008 9:03:58 AM). But the start_date and end_date do not have a time specified from the FORM, so they both display with 12:00:00 AM at the end. So lets say from my FORM, I choose 9-24-2008 for both my start and ending dates. In my query, it looks like...
dbDate BETWEEN 9-24-2008 12:00:00 AM AND 9-24-2008 12:00:00 AM
So if my dbDate's time is anything but 12:00:00 AM, it will not catch it in the comparison. I have to choose 9-25-2008 (9-25-2008 12:00:00 AM) to catch any dates on the 24th (and again, any dates in my database that are 9-25-2008 will now be discluded).
How can write this so the times of the dates are irrelevant? Is there a way to strip the time from dbDate? or to set the time of end_date so it looks like 9-24-2008 11:59:59 PM ? Any help is appreciated.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
1 Correct answer
LEGEND
,
Sep 24, 2008
Sep 24, 2008
Change this
<cfset end_date = #CREATEODBCDATE(toDate)#>
to this
<cfset end_date = #CREATEODBCDATE(dateadd("d", 1,toDate))#>
and this
WHERE dbDate BETWEEN #start_date# AND #end_date#
to this
WHERE dbDate >= #start_date#
AND dbdate < #end_date#
That's the logic. To improve it event more, use cfqueryparam.
<cfset end_date = #CREATEODBCDATE(toDate)#>
to this
<cfset end_date = #CREATEODBCDATE(dateadd("d", 1,toDate))#>
and this
WHERE dbDate BETWEEN #start_date# AND #end_date#
to this
WHERE dbDate >= #start_date#
AND dbdate < #end_date#
That's the logic. To improve it event more, use cfqueryparam.
LEGEND
,
/t5/coldfusion-discussions/cfquery-comparing-same-dates/m-p/168286#M15273
Sep 24, 2008
Sep 24, 2008
Copy link to clipboard
Copied
use your db's date functions to extract just the date part
from your
column and compare that to your form vars. check your db manula for
which date function to use.
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
column and compare that to your form vars. check your db manula for
which date function to use.
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/cfquery-comparing-same-dates/m-p/168287#M15274
Sep 24, 2008
Sep 24, 2008
Copy link to clipboard
Copied
Change this
<cfset end_date = #CREATEODBCDATE(toDate)#>
to this
<cfset end_date = #CREATEODBCDATE(dateadd("d", 1,toDate))#>
and this
WHERE dbDate BETWEEN #start_date# AND #end_date#
to this
WHERE dbDate >= #start_date#
AND dbdate < #end_date#
That's the logic. To improve it event more, use cfqueryparam.
<cfset end_date = #CREATEODBCDATE(toDate)#>
to this
<cfset end_date = #CREATEODBCDATE(dateadd("d", 1,toDate))#>
and this
WHERE dbDate BETWEEN #start_date# AND #end_date#
to this
WHERE dbDate >= #start_date#
AND dbdate < #end_date#
That's the logic. To improve it event more, use cfqueryparam.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
EvolvedDSM
AUTHOR
Engaged
,
LATEST
/t5/coldfusion-discussions/cfquery-comparing-same-dates/m-p/168288#M15275
Sep 24, 2008
Sep 24, 2008
Copy link to clipboard
Copied
Thanks Dan. That did the trick!
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

