简体   繁体   中英

SQL query to delete records when the difference between two dates is greater than a certain value

I would like to delete records when the difference between SYSDATE and a TIMESTAMP (6) field of my table is greater than 10 days. I have created the following query:

select (SYSDATE - myDate) as difference from myTable where difference > 10;

but i get the following error:

00904. 00000 -  "%s: invalid identifier"

am i creating the query correctly?

am i creating the query correctly?

No, you cannot refer to an alias in the SELECT clause in the filter condition of the same statement.

Additionally, when you subtract a TIMESTAMP from a DATE you will get the result as an INTERVAL data type; so you need to compare on that rather than a NUMBER (which would be the result if you compared DATE - DATE ).

You want:

SELECT SYSDATE - myDate as difference
FROM   myTable
WHERE  SYSDATE - myDate > INTERVAL '10' DAY;

db<>fiddle here

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