Solved

Trying to delete from database on datetime criteria

Posted on 2014-02-10
3
273 Views
Last Modified: 2014-02-10
I am trying to delete all records from a table that are older than 12 months.  The date field is a datetime field.  I have the following code:

<CFSET DelDate = DateAdd("yyyy",-1,Now())>

<CFQUERY NAME="DelLogs" DATASOURCE="JobOrder#Sfx#">
  DELETE FROM JOLog
  WHERE TDate <= <CFQUERYPARAM Value="#DelDate#"  CFSQLTYPE="CF_SQL_DATETIME">
</CFQUERY>

When I run the code nothing is deleted.  What am I doing wrong?
0
Comment
Question by:jdthedj
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39848348
EDIT:
Assuming you've verified the table actually contains records older than 1 year ago...

          SELECT COUNT(*) AS TotalMatchingRecords
          FROM    JOLog
          WHERE  TDate <= <CFQUERYPARAM Value="#DelDate#"  CFSQLTYPE="CF_SQL_TIMESTAMP">

Open in new window


If you wish to include the current time, ie remove all records earlier than 2014-02-10 12:03:15 PM use:

....
WHERE TDate <= <CFQUERYPARAM Value="#DelDate#"  CFSQLTYPE="CF_SQL_TIMESTAMP">

Open in new window


To use the date only, ie remove all records earlier than 2014-02-10

....
WHERE TDate < <CFQUERYPARAM Value="#DelDate#"  CFSQLTYPE="CF_SQL_DATE">

Open in new window

0
 
LVL 3

Author Closing Comment

by:jdthedj
ID: 39848368
Thanks agx!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39848377
Welcome :)
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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