602SQL Documentation Index  

EXTRACT Function

EXTRACT ( { DAY | MONTH | YEAR | HOUR | MINUTE | SECOND } FROM arg )

This function returns a specified component of date or time specified by the arg expression. Arg has to be a DATE, TIME or TIMESTAMP type. If arg is NULL, the function returns NULL.

The EXTRACT function returns an integer value except for the EXTRACT(SECOND FROM arg) case, where it returns a real value with 3 decimal places (thousandths of a second). You cannot extract the time zone value. If you are trying to extract a non-existing entry (e.g. MINUTE from the DATE type) the function returns 0. Days and months are counted from 1.

This function partial overlaps the standard functions of the 602SQL language, such as Month, Year, Hours, etc.

Example:

Calculates invoice amounts over months.

SELECT Companies.name, EXTRACT (MONTH FROM date1) AS month, Sum(invoices.amount) AS sum_dollars
FROM Invoices,Companies
WHERE Invoices.Company=Companies.Number
GROUP BY Companies.name, EXTRACT (MONTH FROM date1)
ORDER BY Companies.name, month