Press "Enter" to skip to content

How to Get Quarter From Date in Oracle?

Below I am giving two examples to get quarter from the date in Oracle using PL/SQL and SQL query.

Get Quarter Using PL/SQL Function (Stored Function) in Oracle

The following function takes the date parameter and returns the quarter as the number.

CREATE OR REPLACE FUNCTION get_quarter (p_date IN DATE)
RETURN NUMBER
AS
l_qtr NUMBER;
l_month NUMBER;
BEGIN
l_month := TO_CHAR (p_date, 'mm');

IF l_month IN (1, 2, 3)
THEN
l_qtr := 1;
ELSIF l_month IN (4, 5, 6)
THEN
l_qtr := 2;
ELSIF l_month IN (7, 8, 9)
THEN
l_qtr := 3;
ELSIF l_month IN (10, 11, 12)
THEN
l_qtr := 4;
END IF;

RETURN l_qtr;
END get_quarter;

Test

SELECT get_quarter (SYSDATE) FROM DUAL;

Output

 QUARTER
----------
3
1 row selected.

Get Quarter Using SQL Query in Oracle

SELECT CASE
WHEN TO_CHAR (SYSDATE, 'mm') IN (1, 2, 3) THEN 1
WHEN TO_CHAR (SYSDATE, 'mm') IN (4, 5, 6) THEN 2
WHEN TO_CHAR (SYSDATE, 'mm') IN (7, 8, 9) THEN 3
WHEN TO_CHAR (SYSDATE, 'mm') IN (10, 11, 12) THEN 4
END
quarter
FROM DUAL;

Output

QUARTER
----------
3
1 row selected.

Using TO_CHAR Function

Select TO_CHAR(sysdate, 'Q') from dual;

Output

QUARTER
----------
3
1 row selected.

See also: