ORACLE date function: take the end of last month/beginning/quarter end/quarter beginning/year end/beginning...

Common functions

SYSDATE()

Returns the current date and time.

ADD_MONTHS(date, n)

Returns the date with n months added to the given date.

-- 当前月份加3
SELECT ADD_MONTHS(SYSDATE, 3) FROM DUAL;
-- 当前月份减3
SELECT ADD_MONTHS(SYSDATE, -3) FROM DUAL;

LAST_DAY(date)

Returns the date of the last day of the month in which the given date falls.

SELECT LAST_DAY(TO_DATE('2023-10-01', 'YYYY-MM-DD')) FROM DUAL;

This code returns October 31, 2023, the last day of October 2023.

TRUNC(date)

Returns the truncated value for the given date.

--当前日期所在年份的第一天
SELECT TRUNC(SYSDATE, 'YEAR') FROM DUAL;
SELECT TRUNC(SYSDATE, 'Y') FROM DUAL;

--当前日期所在月份的第一天
SELECT TRUNC(SYSDATE, 'MONTH') FROM DUAL;
SELECT TRUNC(SYSDATE, 'MM') FROM DUAL;

--当前日期所在季度的第一天
SELECT TRUNC(SYSDATE, 'Q') FROM DUAL;

--当前日期所在周的第一天
SELECT TRUNC(SYSDATE, 'IW') FROM DUAL;

EXTRACT

Extract various parts of a date, including year, month, day, etc.

--截取当前年份/月份/日期
SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL;
SELECT EXTRACT(MONTH FROM SYSDATE) FROM DUAL;
SELECT EXTRACT(DAY FROM SYSDATE) FROM DUAL;

--截取当前日期的季度
SELECT TRUNC(TO_NUMBER(TO_CHAR(SYSDATE, 'MM')) / 3.1) + 1 AS QUARTER
FROM DUAL;
--截取当前时间的小时(24小时制)
SELECT TO_CHAR(SYSDATE, 'HH24') AS HOUR FROM DUAL;

ROUND(date)

Returns the rounded value for a given date.
If you want to calculate the rounded value of October 15, 2022 14:30:00, you can use the following code:

SELECT ROUND(TO_DATE('2022-10-15 14:30:00', 'YYYY-MM-DD HH24:MI:SS')) FROM DUAL;

This code returns October 16, 2022 00:00:00, rounding the given date to the nearest day.

MONTHS_BETWEEN(date1, date2)

Returns the difference in months between two dates.

SELECT 
MONTHS_BETWEEN(TO_DATE('2022-12-01', 'YYYY-MM-DD'), 
TO_DATE('2022-10-01', 'YYYY-MM-DD')) FROM DUAL;

Scenario example

Get data at the end of last month/beginning/quarter end/beginning of quarter/year end/beginning of the year

--上月末 
TRUNC(SYSDATE, 'MM') - 1 
--月末
ADD_MONTHS(TRUNC(SYSDATE, 'MM'), 1) - 1
--上月初
ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -1) 
--月初
TRUNC(SYSDATE, 'MM')
--上季末
TRUNC(SYSDATE, 'Q') - 1 
--季末 
ADD_MONTHS(TRUNC(SYSDATE, 'Q'), 3) - 1
--上季初
ADD_MONTHS(TRUNC(SYSDATE, 'Q'), -3) 
--季初
TRUNC(SYSDATE, 'Q')
 --上年末
TRUNC(SYSDATE, 'Y') - 1 
--年末
ADD_MONTHS(TRUNC(SYSDATE, 'Y'), 12) - 1
--上年初
ADD_MONTHS(TRUNC(SYSDATE, 'Y'), -12) 
--年初
 TRUNC(SYSDATE, 'Y')

Output the data of the last day of each month in the last three months

In the T table, dt is the current date parameter of the defined varchar2 type, to filter out all the data on the last day of each month in the last three months in the T table

SELECT *
FROM T
WHERE TO_DATE(dt, 'YYYY-MM-DD') IN (
    LAST_DAY(ADD_MONTHS(TO_DATE(dt, 'YYYY-MM-DD'), -2)),
    LAST_DAY(ADD_MONTHS(TO_DATE(dt, 'YYYY-MM-DD'), -1)),
    LAST_DAY(TO_DATE(dt, 'YYYY-MM-DD'))
);

This code will filter out the last day of each month in the last three months in the table T according to the defined dt parameter (assuming it is a date string of VARCHAR2 type in the format of 'YYYY-MM-DD'). All data and output the result.

We first use the TO_DATE function to convert the datadate column from VARCHAR2 type to DATE type. We then use the ADD_MONTHS and TRUNC functions to calculate the three months prior to the current date, and use the WHERE clause to filter out the data for the last three months.

Delete data that is not at the end of the last 12 months

There are three fields A, B and dt in the T table, and dt is the current date parameter of the defined varchar2 type, so as to delete the data at the end of the last 12 months in the T table

DELETE FROM T
WHERE NOT EXISTS (
    SELECT 1
    FROM (
        SELECT LAST_DAY(ADD_MONTHS(TO_DATE(dt, 'YYYY-MM-DD'), -ROWNUM)) AS MONTH_END
        FROM T
        WHERE ROWNUM <= 12
    )
    WHERE MONTH_END = TO_DATE(dt, 'YYYY-MM-DD')
);

This code will delete the data at the end of the last 12 months in the table T according to the defined dt parameter (assuming it is a VARCHAR2 type date string in the format of 'YYYY-MM-DD').
The following code can also be achieved:

DELETE FROM T
WHERE TO_DATE(dt, 'YYYY-MM-DD') NOT IN (
    SELECT LAST_DAY(ADD_MONTHS(TO_DATE(dt, 'YYYY-MM-DD'), -LEVEL))
    FROM DUAL
    CONNECT BY LEVEL <= 12
);

Update data for the last 7 days of each quarter

In table T, dt is the current date parameter of the defined varchar2 type. If dt is not the last 7 days of each quarter, insert '1' into table T, and update the data in table T2 to '2 '

DECLARE
    dt DATE := TO_DATE(:dt, 'YYYY-MM-DD');
    last_day_of_quarter DATE;
BEGIN
    last_day_of_quarter := ADD_MONTHS(TRUNC(dt, 'Q'), 3) - INTERVAL '1' DAY;
    IF dt < last_day_of_quarter - INTERVAL '6' DAY THEN
        INSERT INTO T VALUES ('1');
        UPDATE T2 SET column_name = '2';
    END IF;
END;

ADD_MONTHS(TRUNC(dt, 'Q'), 3) - INTERVAL '1' DAY is an expression that calculates the last day of the quarter for the given date dt.

First, TRUNC(dt, 'Q') truncates the date dt to the first day of the quarter. For example, if dt is '2022-05-15', the result of TRUNC(dt, 'Q') is '2022-04-01'.

Then, use the ADD_MONTHS function to add 3 months to the truncated date to get the first day of the next quarter. For example, if the truncated date is '2022-04-01', the result of ADD_MONTHS(TRUNC(dt, 'Q'), 3) is '2022-07-01'.

Finally, subtract 1 day from the first day of the next quarter to get the last day of the current quarter. For example, if the first day of the next quarter is '2022-07-01', the result of ADD_MONTHS(TRUNC(dt, 'Q'), 3) - INTERVAL '1' DAY is '2022-06-30'

Supongo que te gusta

Origin blog.csdn.net/qq_43605229/article/details/131301415
Recomendado
Clasificación