When using the TO_CHAR(datetime)
function in Oracle Database, you can use the Q
format element to return the quarter that a given date belongs to.
Examples
Here’s an example to demonstrate:
SELECT TO_CHAR(date '2035-07-20', 'Q')
FROM DUAL;
Result:
3
Here’s an example with various dates throughout the year:
SELECT
TO_CHAR(date '2035-03-20', 'Q') AS "r1",
TO_CHAR(date '2035-05-20', 'Q') AS "r2",
TO_CHAR(date '2035-08-20', 'Q') AS "r3",
TO_CHAR(date '2035-10-20', 'Q') AS "r4"
FROM DUAL;
Result:
r1 r2 r3 r4 _____ _____ _____ _____ 1 2 3 4
Here, I pass SYSDATE
to get the current quarter:
SELECT
SYSDATE AS "Today's Date",
TO_CHAR(SYSDATE, 'Q') AS "Today's Quarter"
FROM DUAL;
Result:
Today's Date Today's Quarter _______________ __________________ 28/AUG/21 3