The EXTRACT function extracts a portion of a DateTime, such as the day of the week or month of the year.
This is useful in situations where the data must be filtered or grouped by a slice of its timestamps, for example computing the total sales that occurred on any Monday.
<ExtractExpr> ::= EXTRACT(<expr>,<DateTimeUnit>)
<DateTimeUnit> ::= SECOND | MINUTE | HOUR | DAY_OF_WEEK |
DAY_OF_MONTH | DAY_OF_YEAR | DATE | WEEK |
MONTH | QUARTER | YEAR
For example, given a DateTime property "TimeStamp" with a value representing 10/13/2009 11:35:12.000, the EXTRACT operator could be used to compute the following results:
EXTRACT("TimeStamp", SECOND) = 12
EXTRACT("TimeStamp", MINUTE) = 35
EXTRACT("TimeStamp", HOUR) = 11
EXTRACT("TimeStamp", DATE) = 13
EXTRACT("TimeStamp", WEEK) = 40 /* Zero-indexed */
EXTRACT("TimeStamp", MONTH) = 10
EXTRACT("TimeStamp", QUARTER) = 3 /* Zero-indexed */
EXTRACT("TimeStamp", YEAR) = 2009
EXTRACT("TimeStamp", DAY_OF_WEEK) = 4 /* Zero-indexed */
EXTRACT("TimeStamp", DAY_OF_MONTH) = 13
EXTRACT("TimeStamp", DAY_OF_YEAR) = 286 /* Zero-indexed */
As a simple example of using this functionality, the following query groups transaction records containing TimeStamp and Amount properties by quarter, and for each quarter computes the total sales that occurred on a Monday (DAY_OF_WEEK=1):
RETURN Quarters AS
SELECT SUM(Amount) AS Total
TRUNC(TimeStamp, QUARTER) AS Qtr
WHERE EXTRACT(TimeStamp,DAY_OF_WEEK) = 1
GROUP BY Qtr

