searching for Hourly criteria in a date time field

bknight
SilverLounger
Posts: 1710
Joined: 08 Jul 2016, 18:53

searching for Hourly criteria in a date time field

Post by bknight »

I've have a date/time field and I want to have a criteria of hours.

Code: Select all

SELECT Hour([Filled]) AS [Time], Trades.Type, Trades.Symbol
FROM Trades
WHERE (((Hour([Filled]))>#12/30/1899 8:0:0# And (Hour([Filled]))<#12/30/1899 9:0:0#) AND ((Trades.Filled)>#12/21/2024#) AND ((Trades.Type)<>"Dividend*" And (Trades.Type)<>"Fee*") AND ((Trades.Symbol)<>"Spy"));
No records are recovered, so obviously there is an error in the criteria.

User avatar
HansV
Administrator
Posts: 80372
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: searching for Hourly criteria in a date time field

Post by HansV »

The Hour function does not return a time value but a number 0 ... 24. To return the time part of a date/time value, you can use the TimeValue function.

For example

Code: Select all

SELECT TimeValue([Filled]) AS [Time], Trades.Type, Trades.Symbol
FROM Trades
WHERE (((TimeValue([Filled]))>#12/30/1899 8:0:0# And (TimeValue([Filled]))<#12/30/1899 9:0:0#) AND ((Trades.Filled)>#12/21/2024#) AND ((Trades.Type)<>"Dividend*" And (Trades.Type)<>"Fee*") AND ((Trades.Symbol)<>"Spy"));
Best wishes,
Hans

bknight
SilverLounger
Posts: 1710
Joined: 08 Jul 2016, 18:53

Re: searching for Hourly criteria in a date time field

Post by bknight »

That did the trick, thanks