Link to home
Start Free TrialLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

Delete query

Hi,

I would like to delete records that have been in the database more than 12 months.

Delete from ADS
where datediff(NOW(),AdsDate) > twelve months?)  (MainCategory = 4 or MainCategory = 5 or MainCategory = 6)
Avatar of Muhammad Khan
Muhammad Khan
Flag of Canada image

Delete from ADS
where period_diff(NOW(),AdsDate) > 12)  (MainCategory = 4 or MainCategory = 5 or MainCategory = 6)
Avatar of lulu50

ASKER

it deleted all my data

Delete from ADS
where (period_diff(NOW(),AdsDate) > 12) and (MainCategory = 4 or MainCategory = 5 or MainCategory = 6)
Avatar of lulu50

ASKER

I only want to delete the date calculated from today's date up to 12 months
ASKER CERTIFIED SOLUTION
Avatar of Cluskitt
Cluskitt
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lulu50

ASKER

Cluskitt:

GREAT!!
Avatar of lulu50

ASKER

thank you
Try this and let me know if it helped :

Delete from ADS
where months_between (to_date (NOW(), 'yyyy/mm/dd'), to_date (AdsDate, 'yyyy/mm/dd') ) > 12
and (MainCategory = 4 or MainCategory = 5 or MainCategory = 6)
oops ...sorry.. my bad.. Hope you are not doing it directly on production..



Delete from ADS
where date_add(AdsDate, interval 12 Month)<Now()  and (MainCategory = 4 or MainCategory = 5 or MainCategory = 6)
while Cluskitt solution will work for most of the cases.. you may find it delete 1st of january record in a leap year due to 366 days in leap year..