Add_months() function parameter -3 in Oracle

Oracle usually uses add_months() to help count the data of the last few months

add_months(sysdate,-1)			--近1个月的数据
add_months(sysdate,-3) 			--近3个月的数据
add_months(sysdate,-6) 			--近6个月的数据

For example, you can query like this


select * from 表名 where acctime<add_months(systimestamp,-3)

The case is as follows:

create table t1 (a timestamp);

insert into t1 values (TO_TIMESTAMP('2012-04-01 12:12:09', 'YYYY-MM-DD HH24:MI:SS'));
insert into t1 values (TO_TIMESTAMP('2012-03-01 12:12:09', 'YYYY-MM-DD HH24:MI:SS'));
insert into t1 values (TO_TIMESTAMP('2012-02-01 12:12:09', 'YYYY-MM-DD HH24:MI:SS'));
insert into t1 values (TO_TIMESTAMP('2012-01-01 12:12:09', 'YYYY-MM-DD HH24:MI:SS'));
insert into t1 values (TO_TIMESTAMP('2012-04-12 12:12:09', 'YYYY-MM-DD HH24:MI:SS'));


select * from t1
where a >= add_months(sysdate,-3) and a <= sysdate


                     a
-----------------------------------------------
1    01-4月 -12 12.12.09.000000 下午
2    01-3月 -12 12.12.09.000000 下午
3    01-2月 -12 12.12.09.000000 下午
4    12-4月 -12 12.12.09.000000 下午

see:

The solution to record the first 3 months in the query table in oracle-Oracle Tutorial-aiyi webpage

add_months() statistics in oracle for the past 1 month, nearly 3 months, and nearly 6 months_oracle query data for the last three months_Data Analyst's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/figosoar/article/details/131591714