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.
moe57Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

UnifiedISCommented:
DELETE FROM table
WHERE datefield < CONVERT(varchar(10), DATEADD(DAY, -2, GETDATE()), 101)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
moe57Author Commented:
it is not deleting anything.  All the data are still there.  Here is how the data look like
2015-03-06 20:30:00.000
2015-03-06 21:00:00.000
2015-03-06 21:30:00.000
2015-03-06 22:00:00.000
2015-03-06 22:30:00.000
2015-03-06 23:00:00.000
2015-03-06 23:30:00.000
2015-03-05 00:00:00.000
2015-03-05 00:30:00.000
2015-03-05 01:00:00.000
2015-03-05 01:30:00.000
2015-03-05 02:00:00.000
2015-03-05 02:30:00.000

Open in new window

0
Scott PletcherSenior DBACommented:
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)
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

UnifiedISCommented:
Those dates are from the last 2 days which you said you wanted to retain. "Everything...except the last 2 days"
0
moe57Author Commented:
thanks
0
UnifiedISCommented:
Was there something wrong with my proposal?
0
moe57Author Commented:
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?
0
UnifiedISCommented:
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.
0
moe57Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.