totn Access Functions

MS Access: Hour Function

This MSAccess tutorial explains how to use the Access Hour function with syntax and examples.

Description

The Microsoft Access Hour function returns the hour of the day (a number from 0 to 23) given a time value.

Syntax

The syntax for the Hour function in MS Access is:

Hour ( time_value )

Parameters or Arguments

time_value
A valid time.

Returns

The Hour function returns a numeric value between 0 and 23.

Applies To

The Hour function can be used in the following versions of Microsoft Access:

  • Access 2019, Access 2016, Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

Example

Let's look at how to use the Hour function in MS Access:

Hour (#10:42:58 PM#)
Result: 22

Hour (#10:42:58 AM#)
Result: 10

Hour (#22/11/2003 10:42:58 PM#)
Result: 22

Example in VBA Code

The Hour function can be used in VBA code in Microsoft Access.

For example:

Dim LHour As Integer

LHour = Hour(#10:42:58 PM#)

In this example, the variable called LHour would now contain the value of 22.

Example in SQL/Queries

You can also use the Hour function in a query in Microsoft Access.

For example:

Microsoft Access

In this query, we have used the Hour function as follows:

Expr1: Hour(#10:42:58 PM#)

and

Expr2: Hour([CategoryDate])

The first Hour function will extract the hour value from the time value 10:42:58 PM and display the results in a column called Expr1. You can replace Expr1 with a column name that is more meaningful.

For example:

HourValue: Hour(#10:42:58 PM#)

The results would now be displayed in a column called HourValue.

The second Hour function will extract the hour value from the CategoryDate field and return the results in a column called Expr2.