MS Access: DatePart Function
This MSAccess tutorial explains how to use the Access DatePart function with syntax and examples.
Description
The Microsoft Access DatePart function returns a specified part of a given date.
Syntax
The syntax for the DatePart function in MS Access is:
DatePart ( interval, date, [firstdayofweek], [firstweekofyear])
Parameters or Arguments
- interval
The interval of time that you wish to return. This parameter can be any one of the following valid interval values:
Interval Explanation yyyy Year q Quarter m Month y Day of year d Day w Weekday ww Week h Hour n Minute s Second - date
- The date value that you wish to evaluate.
- firstdayofweek
is omitted, Access assumes that Sunday is the first day of the week. This parameter can be one of the following values:
Constant Value Explanation vbUseSystem 0 Use the NLS API setting vbSunday 1 Sunday (default) vbMonday 2 Monday vbTuesday 3 Tuesday vbWednesday 4 Wednesday vbThursday 5 Thursday vbFriday 6 Friday vbSaturday 7 Saturday - firstweekofyear
Optional. It is a constant that specifies the first week of the year. If this parameter is omitted, Access assumes that the week containing Jan 1st is the first week of the year. This parameter can be one of the following values:
Constant Value Explanation vbUseSystem 0 Use the NSL API setting vbFirstJan1 1 Use the first week that includes Jan 1st (default) vbFirstFourDays 2 Use the first week in the year that has at least 4 days vbFirstFullWeek 3 Use the first full week of the year
Returns
The DatePart function returns a numeric value.
Applies To
The DatePart 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 DatePart function in MS Access:
DatePart ("yyyy", #15/10/1998#) Result: 1998 DatePart ("m", #15/10/2003#) Result: 10 DatePart ("d", #15/10/2003#) Result: 15
Example in VBA Code
The DatePart function can be used in VBA code in Microsoft Access.
For example:
Dim LValue As Integer LValue = DatePart ("d", #15/10/2003#)
In this example, the variable called LValue would now contain the value of 15.
Example in SQL/Queries
You can also use the DatePart function in a query in Microsoft Access.
For example:
In this query, we have used the DatePart function as follows:
Expr1: DatePart('d',[CategoryDate])
and
Expr2: DatePart('d',#15/10/2003#)
The first DatePart function will extract the day value from the CategoryDate field and display the results in a column called Expr1. You can replace Expr1 with a column name that is more meaningful.
For example:
Days: DatePart('d',[CategoryDate])
The results would now be displayed in a column called Days.
The second DatePart function will extract the day value from the date 15/10/2003 and return the results in a column called Expr2.
Advertisements