按访问日期删除 [英] Delete by Date In Access

查看:54
本文介绍了按访问日期删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日期的字段,我需要做的是删除2岁的日期,因此,如果某个字段中的日期为2014年6月16日,则将其删除,即6/16/2013将保留它.有可能吗?

I have a field that contains a date in it, and what i need to do is to delete dates that are 2 years old, so if a date in a field is 6/16/2014 in will delete it, 6/16/2013 will keep it. Is it possible?

推荐答案

像这样的SQL查询可以解决问题,删除2013年6月17日或之后的任何内容(我认为这是您想要的).

A SQL query like this should do the trick, deleting anything 6/17/2013 or after (which I think is what you were looking for).

DELETE FROM MyTableName WHERE MyDateField >= #6/17/2013#

我发现使用>= #6/17/2013#而不是> #6/16/2013#是更安全的,因为经常指定没有时间的日期意味着该天是午夜,所以在6/16午夜之后发生的任何事情仍被认为是之后"并得到删除.

I find it's safer to use >= #6/17/2013# rather than > #6/16/2013#, since often specifying a date without a time means that day at midnight, so anything that happened after midnight on 6/16 is still considered "after" and gets deleted.

此外,为防万一,在进行批量删除之前,您可能希望复制.mdb文件. :-)

Also, you might want to make a copy of your .mdb file before you go doing mass deletes, just in case. :-)

要进一步阅读,请查看

For further reading, check out Office.com: Access SQL: basic concepts, vocabulary, and syntax

编辑:要自动使用今天的日期减去2年,查询将变为

To automatically use today's date minus 2 years, the query would become

DELETE FROM MyTableName WHERE MyDateField > DATEADD("yyyy", -2, DATE())

课程,这可能还会删除事件6/16/2013及之后,而不是事件6/17/2013及之后.如果存在问题,则可以使用两个嵌套的DATEADD语句对其进行调整,一个语句减去2年,而一个语句减去2天.

Course, that might also delete events 6/16/2013 and after, rather than 6/17/2013 and after. If that's a problem, you could tweak it with either nested DATEADD statements, one to subtract 2 years and one to add a day.

FMI:访问DATEADD()函数

这篇关于按访问日期删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆