Extracting Components of Dates, Times, and Durations

A number of functions allow you to extract specific parts of dates, times, and durations. For example, to retrieve the year part of a date, you can use the year-from-date function. Table 19-5 lists all the component extraction functions for date, time, and duration values.

Table 19-5. Component extraction functions

year-from-dateTime

day-from-dateTime

minutes-from-dateTime

year-from-date

day-from-date

minutes-from-time

years-from-duration

minutes-from-duration

days-from-duration

month-from-dateTime

hours-from-dateTime

seconds-from-dateTime

month-from-date

hours-from-time

seconds-from-time

months-from-duration

hours-from-duration

seconds-from-duration

When working with duration values, these functions calculate the result based on the canonical representation of the values. For example, the function years-from-duration does not necessarily return the integer that appears before the Y in the original value. Rather, it returns the number of whole years in the duration, taking into account that the number of months might be more than 12. If the xs:duration value is P1Y15M, the function returns 2, not 1. This is because the canonical representation of P1Y15M (1 year and 15 months) is actually P2Y3M (2 years and 3 months). To extract the number of years as a decimal number, you could instead divide the value by the duration P1Y, which would return 2.25. This is described ...

Get XQuery now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.