MS Access SQL select by hour of day

Hi there I am trying to write a reusable SQL statement that I can run daily where I can select all the records in my table before 13:00:00 each day.
My date format is stored as a date time stamp I have looked at using the date part function but I can't get it working.

SELECT datePart("hh",CustTable.[Log Time]) AS Hour
FROM faculty2013
WHERE ((CustTable.[Booklet Received])=1 and hour <= '13:00:00');
LVL 7
jay_eireAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ButlerTechnologyCommented:
You can't use an alias in a where clause.  You can rewrite the function in the where clause
SELECT datePart("hh",CustTable.[Log Time]) AS Hour
FROM faculty2013
WHERE ((CustTable.[Booklet Received])=1 and datePart("hh",CustTable.[Log Time])<= '13:00:00'); 

Open in new window


The alias can be used in the Order By Clause.

Tom
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
If this is an Access query, and we're talking 1pm on the day it's executed..
SELECT yada, yada, yada
FROM faculty2013
WHERE CustTable.[Booklet Received] = 1
  AND SomeDateColumn <= CDate(Date() & " 13:00:00")

Open in new window

0
jay_eireAuthor Commented:
Thanks tom does the time part look correct in my SQL statement will that work on a date time stamp column?
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

ButlerTechnologyCommented:
After looking it over, you should make a few minor adjustments.  The Datepart will return an integer, so you can change the '13:00:00' to just be 13.

Tom
0
jay_eireAuthor Commented:
@tom I tried that statement but I'm getting back an error or Invalid procedure call I checked the first part of the query without the where clause and hour is being selected as #error
0
jay_eireAuthor Commented:
@jimhorn thanks for reply I checked that statement but it is returning all the rows in my table from all dates.
I'm trying to get the query to select the rows based on the current date only and before 13:00:00.
0
ButlerTechnologyCommented:
I adjusted the query provide by Jim to only show the current day's results:
SELECT yada, yada, yada
FROM faculty2013
WHERE CustTable.[Booklet Received] = 1
 AND SomeDateColumn between CDate(Date() & " 00:00:00")  and CDate(Date() & " 13:00:00") 

Open in new window


Tom
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jay_eireAuthor Commented:
Thanks so much both are you ok for me to split the points?
0
ButlerTechnologyCommented:
I am good either way -- Jim did the heavy lifting.

Tom
0
jay_eireAuthor Commented:
Thanks both for the Swift Response, I split points. Hope this is OK, if not let me know.

J
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Good enough.  Thanks for the split, good luck with your project.  -Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.