There are several ways you can extract only the number of hours from an MS Access Date
value in a query expression a few ways.
Here are some of the methods:
DatePart("h", [yourFieldName]) 'returns a variant (Integer)
Format([yourFieldName],"h") 'returns a variant (String)
Hour([yourFieldName]) 'returns a variant (Integer)
In Access, I prefer the last method because it's short and clear.
Based on the question, I suspect you were looking at the documentation for the SQL Server DATEPART
function which has some subtle differences.
Don't make the mistake of thinking that SQL is all the same. There are similarities but the syntax is not always interchangeable, especially when talking about MS Access. When searching for answers about Access, I always include mc access
as part of my Google search query, and even still, when you find a potential answer, confirm that the page you're looking at was actually written for Access.
Your NULL problem
There's something you didn't ask for help for, but that you need help with! :) Indeed this is an old post, so you must have either figured out your mistake, or you're still stuck on it, or the company has been putting out incorrect reports for the last 6 years.
[myFieldName] Is Not Null
is not the same as [myFieldName] <> Null
.
The short explanation is that Null is not a value. Null can things around it. It's not a zero, it's not a ''
empty string, and it's not a ZLS (zero-length-string).
The way you were using the <> NULL
criteria it's likely your query was returning no records because of the incorrect syntax.
Examples:
For example: what result do you think this simple equation will produce?
(Null+2)*3
...if you said 6
, you're wrong!
The result is: NULL
. Basically, anything that touches NULL
becomes NULL
.
One more example. Run this in VBA:
If Null = Null Then MsgBox "Null equals Null." Else MsgBox "Null does NOT equal Null."
...you might not get the answer you'd expect!
For accuracy, all operators must be used the way they were intended, including the use of Is
and Is Not
operators with the NULL
Statement.
More Information:
Preparing for Unexpected Data:
This is actually only barely related, but made me LMAO because I never woudl have thought to prepare for this, and I figured I ought to share:

...which, in return, reminds me of that annoying little kid, Bobby Tables.