Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving data via date 2

Status
Not open for further replies.

peter11

Instructor
Mar 16, 2001
334
0
0
US
I want to pull data from a data base according to date but my date field is a date/time field.

I tried the following but you cannot use date format in the query. There must be a simple way to do this but I can't see it.

<cfset todaydate = #CreateODBCDate(Now())#>
<cfset currentdate = #DateFormat(todaydate, &quot;mm/dd/yyyy&quot;)#>

<cfquery name=&quot;today&quot; datasource=&quot;mycalendar&quot;>
select *
from EventsOneTime
where EventDate = #currentdate#
</cfquery>
 
What database? Try putting single quotes around you date variable, like this: '#currentdate#'



Hope This Helps!

Ecobb
- I hate computers!
 
I only do one step less, but I don't know if it is better or worse.

<cfset currentdate = #DateFormat(Now(), &quot;mm/dd/yyyy&quot;)#>
<cfquery name=&quot;today&quot; datasource=&quot;mycalendar&quot;>
select *
from EventsOneTime
where EventDate = #currentdate#
</cfquery>

You could write a UDF,custom tag,... to format the date and call it in the query.

Kris
 
The problem is that the data is an access database, and the field is date/time.
Below is an example of an entry:
9/10/2003 8:00:00 PM

The above entry is compared with:
10/14/03

I need to eliminate the time aspect of the entry (via code because the database is already populated).

Peter
 
oh, then you need to search for a range.

from EventsOneTime
where EventDate >= '#currentdate# 12:00:00 AM'
and EventDate <= '#currentdate# 11:59:59 PM'

I think something like that would work, I would check the times to make sure I was looking at my clock correctly.

Kris
 
That makes total sense but the single quotes creates the problem to type mismatch.
 
I haven't dealt with Access in so long, I'm not even sure if this would work:

<cfquery name=&quot;today&quot; datasource=&quot;mycalendar&quot;>
select *
from EventsOneTime
where Month(EventDate) = Month(#currentdate#)
AND Day(EventDate) = Day(#currentdate#)
AND Year(EventDate) = Year(#currentdate#)
</cfquery>

Yeah, I know it's nasty, but try it and see if it works.

Hope This Helps!

Ecobb
- I hate computers!
 
Are you getting any errors?

Hope This Helps!

Ecobb
- I hate computers!
 
AS krisbrixon's reply, but replace the single quotes as follows

where EventDate >= ###currentdate## 12:00:00 AM#
and EventDate <= ###currentdate## 11:59:59 PM#
 
Ray,
This gives me the following error:

Invalid parser construct found on line 9 at position 48. ColdFusion was looking at the following text:

12
 
Here is my most recent attempt and the error generated:

===========Code=========
<cfquery name=&quot;today&quot; datasource=&quot;greatwebcalendar&quot;>
select *
from EventsOneTime
where EventOneDateTimeStart >= (#currentdate# 12:00:00 AM)
and EventOneDateTimeStart <= (#currentdate# 11:59:59 PM)
</cfquery>

============Error=========
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'EventOneDateTimeStart >= (10/14/2003 12:00:00 AM) and EventOneDateTimeStart <= (10/14/2003 11:59:59 PM)'.
 
Try this:

<cfquery name=&quot;today&quot; datasource=&quot;greatwebcalendar&quot;>
select *
from EventsOneTime
where EventOneDateTimeStart >= '#currentdate# 12:00:00 AM'
and EventOneDateTimeStart <= '#currentdate# 11:59:59 PM'
</cfquery>



Hope This Helps!

Ecobb
- I hate computers!
 
No, when I use single quotes I get the following error:
[Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
 
Here is a new attempt. The problem here is &quot;this date&quot; is set to the last value in the eventdatetimestart field. I want it to represent every entry in that field as it loops.



<cfset todaydate = #CreateODBCDate(Now())#>
<cfset currentdate = #DateFormat(todaydate, &quot;mm/dd/yyyy&quot;)#>


<cfquery name=&quot;list&quot; datasource=&quot;greatwebcalendar&quot;>
select eventonedatetimestart
from EventsOneTime
</cfquery>
<cfloop query=&quot;list&quot;>
<cfoutput>
<cfset thisdate = #DateFormat(eventonedatetimestart, &quot;mm/dd/yyyy&quot;)#>
<cfquery name=&quot;today&quot; datasource=&quot;greatwebcalendar&quot;>
select eventonedatetimestart
from EventsOneTime
where #currentdate# = #thisdate#
</cfquery>
</cfoutput>
</cfloop>
 
I guess I'm confused why you're using DateFormat after your CreateODBCDate. If you're comparing against a Date/Time field, you actually want the value in ODBC format.

By calling DateFormat(), you're converting your date into a string... which ain't what you want.

Have you tried it without the DateFormat?
Code:
<cfset todaydate = #CreateODBCDate(Now())#>
 
<cfquery name=&quot;today&quot; datasource=&quot;mycalendar&quot;>
select *
from EventsOneTime
where EventDate = #todaydate#
</cfquery>

It's supposed to work ;-)

The nice thing about Date/Time data is the time portion is assumed. If it's absent in the SQL, the driver should automatically assume that you're talking about midnight. So you don't need to do any further formatting.

If you need greater control over the time portion, MX has CreateODBCDateTime()... but I don't think that's available in 5.0 or below.



-Carl
 
&quot;The nice thing about Date/Time data is the time portion is assumed. If it's absent in the SQL, the driver should automatically assume that you're talking about midnight. So you don't need to do any further formatting.&quot;

But we don't know about the values in the database. They could include times. I'm assuming they in fact do include times. You query will only return records where the time is midnight.
 
If all the times in the db are set to midnight, then Carl's version works.

If not, I believe this will do it:
<cfset begindate = #CreateDateTime(DatePart('yyyy', Now()), DatePart('m', Now()), DatePart('d', Now()), 0, 0, 0)#>
<cfset enddate = #CreateDateTime(DatePart('yyyy', Now()), DatePart('m', Now()), DatePart('d', Now()), 23, 59, 59)#>

<cfquery name=&quot;today&quot; datasource=&quot;mycalendar&quot;>
select *
from EventsOneTime
where EventDate >= #begindate# and EventDate <= #enddate#
</cfquery>
 
But now your back to a ColdFusion date... which doesn't always work with ODBC drivers... which is why CreateODBCDate() was, well, created.

Better to use CreateODBCDate and let it do it's time assumption thing:
Code:
<CFSET dtCFTodaysDate = now()>
<CFSET dtTodayAtMidnight = CreateODBCDate(dtCFTodaysDate)>
<CFSET dtTomorrowAtMidnight = CreateODBCDate(DateAdd('d',1,dtCFTodaysDate))>

<CFQUERY name=&quot;today&quot; datasource=&quot;mycalendar&quot;>
   SELECT *
   FROM EventsOneTime
   WHERE EventDate >= #dtTodayAtMidnight# 
   AND EventDate < #dtTomorrowAtMidnight#
</CFQUERY>
Will return all events that have a date between today @ 00:00 and tonight at 23:59.


-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top