How to Delete MSSQL Table Datetime GETDATE() Columns if they are One Year Old or Older Using PHP

感情迁移 提交于 2019-12-13 04:41:39

问题


My column is named order_date and the data for it is:

datetime NOT NULL DEFAULT GETDATE(),

This will make data appear as:

Apr 8 2014 3:24PM

I am wondering what PHP Query I can use to delete any rows in this MSSQL table if they are one year old or older.

I would also like a way to test if this works. (IE: Modifying a current row's order_date column to make it a year older.)

If anyone can help me with this it is greatly appreciated. T


回答1:


delete from tracking_orders where DATEDIFF (year, order_date, GETDATE()) > 1

Check here:

http://technet.microsoft.com/en-us/library/ms189794.aspx




回答2:


DELETE FROM TABLE_NAME 
WHERE order_date < DATEADD(YEAR, -1, GETDATE())

This query can make use of indexes defined on order_date column if any.



来源:https://stackoverflow.com/questions/22948535/how-to-delete-mssql-table-datetime-getdate-columns-if-they-are-one-year-old-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!