moe57
asked on
delete all records except the last 2 days using sql
I am trying to delete everything from my table except the last 2 days; also excluding today's data. the data type for the date field is datetime.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If the table is not too large, you can delete them directly:
DELETE FROM table_name
WHERE datetime_column < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 2, 0)
DELETE FROM table_name
WHERE datetime_column < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 2, 0)
Those dates are from the last 2 days which you said you wanted to retain. "Everything...except the last 2 days"
ASKER
thanks
Was there something wrong with my proposal?
ASKER
UnifiedIS,
No nothing was wrong your proposal and i meant to give you all the points. I thought you were the only one who answered my questions and i did not look at the names. Sorry but how can i correct my mistake now?
No nothing was wrong your proposal and i meant to give you all the points. I thought you were the only one who answered my questions and i did not look at the names. Sorry but how can i correct my mistake now?
I think EE has to reopen it once it's been solved but I don't see the link for it. It's not that big of a deal, I'll survive without the points. I'm glad we were able to help you.
ASKER
thanks
ASKER
Open in new window