简体   繁体   English

SQL 查询两个日期之差大于某个值时删除记录

[英]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.当我的表的 SYSDATE 和 TIMESTAMP (6) 字段之间的差异大于 10 天时,我想删除记录。 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.不,您不能在同一语句的过滤条件中SELECT子句中的别名。

Additionally, when you subtract a TIMESTAMP from a DATE you will get the result as an INTERVAL data type;此外,当您从DATE中减去TIMESTAMP时,您将获得INTERVAL数据类型的结果; so you need to compare on that rather than a NUMBER (which would be the result if you compared DATE - DATE ).所以你需要比较它而不是一个NUMBER (如果你比较DATE - DATE这将是结果)。

You want:你要:

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

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java中两个日期的天数差大于某个值时删除记录 - Delete records when the difference in days between two dates is greater than a certain value in java SQL查询返回两个最近日期的记录之间的差额 - SQL Query to return the difference between records of two most recent dates PostgreSQL - 如何查找两个日期之间的差异大于 1 天/月的记录? - PostgreSQL - how to find records where the difference between two dates is greater than 1 day/month? 我的 SQL 查询两个日期之间的差异? - My SQL query Difference between two Dates? sql查询两个日期之间的日期差 - sql query date difference between two dates SQL 查询将行划分为滞后(行之间的差异)大于某个值的组 - SQL query to partition rows into groups where lag (difference between rows) is greater than some value SQL删除重复项,两列之间的差异较大 - SQL Delete Duplicates with greater difference between two columns SQL SELECT两天之间的差异大于1天 - SQL SELECT Difference between two days greater than 1 day 计算两条记录之间的时间差,如果差值大于 30 秒,则插入另一个表 - Calculate time difference between two records and insert into another table if difference is greater than 30 seconds 使用 sql 删除记录,如果它大于某个数字并附加一个单位(例如 3M 或 3cm) - Delete records using sql ,if it is greater than certain number with an unit attached to it (eg 3M or 3cm)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM