MS Query and Time Part of Timestamp

SPMRYT

New Member
Joined
Jul 29, 2008
Messages
2
I am trying to query an external database where I only want to return the rows that fall between the hours of 11 AM and 3 PM. I can't for the life of me figure out how to set up the criteria in MS Query to bring back the rows that meet the 11 AM to 3 PM window. The column being queried is a timestamp with date and time present. Any help will be greatly appreciated. Thanks.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
hi, What about something like "WHERE Hour(your_field) > 11 AND Hour(your_field) < 15" ? Fazza
 
Upvote 0
The external database is Oracle and it did not like HOUR. I have been trying to use the "TO_DATE" operator but there does not seem to be a way to ignore the date portion of the column in my criteria. I tried using a wild card for the date,
e.g. to_date('%%-%%-%%%% 11:00','MM-DD-YYYY HH:MI')
but that only generated an error.
 
Upvote 0
Within MS Excel & Access, HOUR(CDate(field_name)) should be ok. I don't know anything specific about querying Oracle, sorry. I don't know TO_DATE either. I wonder is this better addressed in an Oracle forum?
 
Upvote 0
may I suggest, the cleanest way is something like
...
WHERE DATEPART("h",your_timestamp_variable)>=11:00 AND
DATEPART("h",your_timestamp_variable)>=15:00

That is how you are supposed to parse date and time combined.

There is also a rather peculiar identifier for query that has the identifier 'td' before the string to id it as a timestamp, but that is for elapsed dates, not what we have here.

This is from memory- plenty of references and examples on the web. Should work with oracle, informix, etc.
 
Last edited:
Upvote 0
I thought I had better check and get it right. The proper SQL syntax for a timestamp in MSQuery is as follows; we happen to have 6 digit 24 hour time; you can alter as need be. the variable event_start and event_end was the date only, but we had to account for time in the tables.... also note... curly braces:

Hope this helps

AND (cc.occ_date>={ts '" & event_start & " 00:00:00'}) AND (cc.occ_date<={ts '" & event_end & " 23:59:59'})
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top