Skip to main content
Previous sectionNext section

QUARTER

A date function that returns the quarter of the year as an integer for a date expression.

Synopsis

{fn QUARTER(date-expression)}

Arguments

Argument Description
date-expression An expression that is the name of a column, the result of another scalar function, or a date or timestamp literal.

Description

QUARTER returns an integer from 1 to 4. The quarter is calculated for an InterSystems IRIS date integer, a $HOROLOG or $ZTIMESTAMP value, an ODBC format date string, or a timestamp.

A date-expression timestamp can be either data type %Library.PosixTimeOpens in a new window (an encoded 64-bit signed integer), or data type %Library.TimeStampOpens in a new window (yyyy-mm-dd hh:mm:ss.fff).

The time periods for the four quarters are as follows:

Quarter Period (inclusive)
1 January 1 to March 31 (90 or 91 days)
2 April 1 to June 30 (91 days)
3 July 1 to September 30 (92 days)
4 October 1 to December 31 (92 days)

QUARTER is based on the month portion of a datetime string. However, all of date-expression is validated and must include a month within the range 1 through 12 and a valid day value for the specified month and year. Otherwise, an SQLCODE -400 error <ILLEGAL VALUE> is generated. The time portion of date-expression can be omitted, but if present must be valid.

The same quarter information can be returned by using the DATEPART or DATENAME function. You can use the DATEADD or TIMESTAMPADD function to increment a date by a specified number of quarters.

This function can also be invoked from ObjectScript using the QUARTER()Opens in a new window method call:

$SYSTEM.SQL.Functions.QUARTER(date-expression)

Examples

The following examples both return the number 1 because the date (February 22) is in the first quarter of the year:

SELECT {fn QUARTER('2018-02-22')} AS ODBCDateQ
Copy code to clipboard
SELECT {fn QUARTER(64701)} AS HorologDateQ
Copy code to clipboard

The following examples all return the current quarter:

SELECT {fn QUARTER({fn NOW()})} AS Q_Now,
       {fn QUARTER(CURRENT_DATE)} AS Q_CurrD,
       {fn QUARTER(CURRENT_TIMESTAMP)} AS Q_CurrTstamp,
       {fn QUARTER($ZTIMESTAMP)} AS Q_ZTstamp,
       {fn QUARTER($HOROLOG)} AS Q_Horolog
Copy code to clipboard

See Also

FeedbackOpens in a new window