如何在SQL查询中使用当前时间戳(Oracle)

时间:2018-10-11 10:16:37

标签: sql oracle

我想运行查询以显示30天之前和30天之后的数据,我知道我应该使用CURRENT TIMESTAMP来执行此操作,并且我可以在30天之前运行数据,但不能在30天之后运行数据。下面是我的查询,请告知这种情况。谢谢!

COB.COB_FA  > CURRENT TIMESTAMP - 30 days and COB.COB_FA  > CURRENT TIMESTAMP + 30 days

4 个答案:

答案 0 :(得分:0)

以“当前时间戳(+/-)”间隔“ 30”天尝试以下

COB.COB_FA  < current_timestamp - interval '30' day or COB.COB_FA  > current_timestamp + interval '30' day

答案 1 :(得分:0)

我认为您只是想要spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57InnoDBDialect

or

答案 2 :(得分:0)

sysdate可以做到这一点。

sysdate-30和sysdate + 30之间的COB.COB_FA

答案 3 :(得分:0)

这将起作用:

create table table_date(dd date);
insert into table_date values(trunc(sysdate));

insert into table_date values(trunc(sysdate)+1);

insert into table_date values(trunc(sysdate)+2);

insert into table_date values(trunc(sysdate)+9);

insert into table_date values(add_months(trunc(sysdate),-5));

//inserting more of them

alter session set nls_date_format = 'dd/MON/yyyy hh24:mi:ss';
SELECT * FROM table_date order by dd;

11/MAY/2018 00:00:00
11/JUL/2018 00:00:00 
11/OCT/2018 00:00:00
12/OCT/2018 00:00:00
13/OCT/2018 00:00:00
18/OCT/2018 00:00:00
20/OCT/2018 00:00:00
11/JAN/2019 00:00:00
11/MAR/2019 00:00:00

SELECT * FROM table_date where dd>add_months(current_timestamp,1) or 
dd<add_months(current_timestamp,-1);

示例输出:

11/JAN/2019 00:00:00
11/MAR/2019 00:00:00
11/JUL/2018 00:00:00
11/MAY/2018 00:00:00