4. Elements of OpenSQL Statements : Functions : Scalar Functions : Date_part Function
 
Share this page                  
Date_part Function
This function is useful in set functions and in assuring correct ordering in complex date manipulation. For example, if date_field contains the value 23-oct-1998, then:
date_part('month',date(date_field))
returns a value of 10 (representing October), and
date_part('day',date(date_field))
returns a value of 23.
Months are numbered 1 to 12, starting with January.
Hours are returned according to the 24-hour clock.
Quarters are numbered 1 through 4.
Week 1 begins on the first Monday of the year. Dates before the first Monday of the year are considered to be in week 0. However, if you specify ISO-Week, which is ISO 8601 compliant, the week begins on Monday, but the first week is the week that has the first Thursday. The weeks are numbered 1 through 53.
Therefore, if you are using Week and the date falls before the first Monday in the current year, date_part returns 0. If you are using ISO-Week and the date falls before the week containing the first Thursday of the year, that date is considered part of the last week of the previous year, and date_part returns either 52 or 53.
The following table illustrates the difference between Week and ISO-Week:
Date Column
Day of Week
Week
ISO-Week
02-jan-1998
Fri
0
1
04-jan-1998
Sun
0
1
02-jan-1999
Sat
0
53
04-jan-1999
Mon
1
1
02-jan-2000
Sun
0
52
04-jan-2000
Tue
1
1
02-jan-2001
Tue
1
1
04-jan-2001
Thu
1
1