简体   繁体   中英

Oracle get records updated in last one hour

Below is the query I am running to get updates in last one hour.

select count(*) 
from my_table 
where last_updated_date between to_date(to_char(sysdate,'YYYY-MM-DD HH24'))-1/24 and to_date(to_char(sysdate,'YYYY-MM-DD HH24'));

Our DB is oracle and it is failing with

ORA-01861: literal does not match format string
01861. 00000 -  "literal does not match format string"
*Cause:    Literals in the input must be the same length as literals in
           the format string (with the exception of leading whitespace).  If the
           "FX" modifier has been toggled on, the literal must match exactly,
           with no extra whitespace.
*Action:   Correct the format string to match the literal.

What is the reason for this failure?

只需从 sysdate 中减去 1/24 即可得到 1 小时前的时间

select count(*) from my_table where last_updated_date >= (sysdate-1/24)

If your db user doesn't have access to sysdate there is another way

select count(*) from my_table where last_updated_date >= CURRENT_TIMESTAMP - INTERVAL '3600' SECOND

You can change SECOND to HOURS MINUTES DAYS etc and time depending on your variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM