日期:2014-05-17  浏览次数:21637 次

oracle中查询表中前3个月记录
A 表中有个日期字段 ,想查询出其前三个月的记录。日期字段类型为TIMESTAMP。如何写sql.请教了!!

------解决方案--------------------
select * from 表名 where acctime<add_months(systimestamp,-3)
------解决方案--------------------
SQL code

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 下午