Solved

SQL query to delete records by date

Posted on 2007-12-04
4
7,308 Views
Last Modified: 2012-05-05
I am looking to write an SQL query that will delete all records either by a single date or between a range of dates.  What is the proper way to write these queries?

TIA for any help!
0
Comment
Question by:dstjohnjr
  • 2
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20406651
DELETE yourtable
WHERE yourfield >= convert(datetime, '2007-12-04', 120)
AND yourfield < dateadd(day, 1, convert(datetime, '2007-12-04', 120))
0
 

Author Comment

by:dstjohnjr
ID: 20406844
Thanks for the reply angellll.

A couple of clarifications:

it should be DELETE FROM yourtable, correct?

Also, can you please explain what the numbers 120 you've used and 1 (before the convert in the last line) are used for?

Thanks again!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20406886
120 stands for the style YYYY-MM-DD
the 1 stays for the 1 day to be added to the date value.

see, when you use 2007-12-04 for both start and end date, it would not return/delete any records, you have to add 1 to the "end date" when comparing using <.
0
 

Author Comment

by:dstjohnjr
ID: 20407107
Thanks Angellll!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question