SQL delete Query

I have a database called "test" that has a table within it called "5Cycle". We have hundreds of thousands of records that I want to archive down. I'd like to do this by year so I need to delete anything older than a certain date. The date column is "TestDate" formatted YYYY-MM-DD   00:00:00:000

How do I select and delete anything older than 1/1/2010?
Keef4000Asked:
Who is Participating?
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Holy {insert diety of choice here} make sure you have a backup, then...

>How do I select and delete anything older than 1/1/2010?
Here's the delete.  Not sure what 'select and delete' means..

USE Test
GO

DELETE FROM [5cycle] WHERE testdate < '2010-01-01'
GO

Open in new window

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
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>that I want to archive down
btw 'archive' implies that there would be an insert from the production table to some kind of archive table before the delete.

Assuming a different database on the same server, that would be this block first, then the delete.

INSERT INTO archive_database_name.[5Cycle]
SELECT * FROM prod_database_name
WHERE testdate < '2010-01-01'

Open in new window

Even better would be to throw the whole thing into a transaction
BEGIN TRY
   BEGIN TRAN tr
   -- archive
   INSERT INTO archive_database_name.[5Cycle]
   SELECT * FROM prod_database_name
   WHERE testdate < '2010-01-01'

   -- delete
   DELETE FROM [5cycle] WHERE testdate < '2010-01-01'
   
   -- if code execution makes it here, good to go. 
   COMMIT TRAN tr
END TRY

BEGIN CATCH 
   -- if code execution makes it here, an error occured, so kill the transaction
   ROLLBACK TRAN tr
END CATCH

Open in new window

0
Mike EghtebasDatabase and Application DeveloperCommented:
It might be necessary to use:

WHERE testdate < '2010-01-01 59:59:59.997'

to make sure all testdate values are effected (i.e. '2010-01-01 59:40:00')

You can also use:

WHERE CONVERT(VARCHAR(10), [testdate], 101) < '2010-01-01'

but the query will be slower.


Mike
0
Keef4000Author Commented:
Thank you that is exactly what I needed. I like the move the record to an archived database better than just backing up and deleting the record. I'm going to head in that direction instead.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
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 2005

From novice to tech pro — start learning today.