473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need to delete MySQL records from a table that are older than 700 days

DTeCH
23
Hello all,

I need to delete records from a table in a MySQL database that are more than 700 days old.

There is a column with the time of the record's entry "pPostDate", & the dates are in RFC 2822 Format... the column's format is VARCHAR, not date or datetime.

The date record looks like this... Sun, 08 May 2011 14:21:50 +0000.

I have tried a bunch of crap (MySQL, or databases of any kind are not my strongest point)

Here are a few queries I have tried:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM cu_info.pi_search WHERE pPostDate BETWEEN DATE_FORMAT(NOW(),'%a, %m %b %Y %T %z') AND DATE_SUB(DATE_FORMAT(NOW(),'%a, %m %b %Y %T %z'), INTERVAL 700 DAY);
  2.  
  3. SELECT * FROM cu_info.pi_search WHERE DATE_SUB(pPostDate, INTERVAL 700 DAY);
  4.  
  5. SELECT * FROM cu_info.pi_search WHERE DATE_SUB(DATE_FORMAT(pPostDate,'%a, %m %b %Y %T %z'), INTERVAL 700 DAY);
  6.  
I know that the queries do not include delete statements... I do not want to mess with that function until I am able to successfully access/return the records to delete.

Any help is greatly appreciated.
Oct 24 '11 #1
5 3009
Rabbit
12,516 Expert Mod 8TB
DATE_FORMAT is used to convert a datetime to a string. Not the other way around. You need to use CONVERT for that.
Expand|Select|Wrap|Line Numbers
  1. SELECT stringDate
  2. FROM someTable
  3. WHERE CONVERT(stringDate, DATETIME) BETWEEN DATE_SUB(NOW(), INTERVAL DAYS 700) AND NOW()
You may or may not have to strip off the time zone stuff first.
Oct 24 '11 #2
DTeCH
23
Hey Rabbit, Thanks for your reply.

Your solution looks correct, except that the DateTime format in my case is RFC 2822... eg: Sun, 08 May 2011 14:21:50 +0000, not "2011-06-04 07:9:22"

I've tried converting it on the fly, but I'm not very savy in MySQL, so not very knowledgeable of it's built in functions.

You see, the problem is, the database has over 40 million entries already, but the table that I am using this delete feature on has 6 million, & it's already tied into software, & websites in circulation. So I just can't change the dateTime column to date format, because the Hash of the table's entries will change, & throw off the functionality of everything that depends on it. I want to leave it as is, & maybe add a new column, & have that one be another date format that MySQL functions are used to.

I didn't realize that MySQL didn't understand dates in RFC 2822 when I originally began.

Thanks again for your solution, It works, just not for my case of RFC 2822 Date Format.
Oct 25 '11 #3
Rabbit
12,516 Expert Mod 8TB
Try substringing out everything from character 6 onwards and dropping the timezone before the convert.
Oct 26 '11 #4
DTeCH
23
Thanks again Rabbit,

I tried your suggestion...
Expand|Select|Wrap|Line Numbers
  1. SELECT pHASH FROM cu_info.wc_search WHERE CONVERT(pPostDate, DATETIME) BETWEEN DATE_SUB(NOW(), INTERVAL 700 DAY) AND NOW();
  2.  
  3. SELECT pHASH FROM cu_info.wc_search WHERE CONVERT(pPostDate, DATETIME) NOT BETWEEN DATE_SUB(NOW(), INTERVAL 700 DAY) AND NOW();
  4.  
however, it wouldn't work no matter how I re-wrote it with those statements. It did return results, but only the ones I didn't want.

So I decided to drop trying to deal with the 2822 formatted datetime column, & added a new one pUnixTimeStamp, & updated it with the row's pPostDate 2822 formatted datetime record converted to Unix TimeStamp (outside of MySQL).

I used the DATE_SUB function you suggested...
Expand|Select|Wrap|Line Numbers
  1. DATE_SUB(NOW(), INTERVAL 700 DAY))
But it had to be converted to TimeStamp to be used against pUnixTimeStamp,
You said to use the CONVERT function, so I snooped around, & found another converting function... UNIX_TIMESTAMP()

I placed the above functions inside UNIX_TIMESTAMP(), & it worked.

Here is the final string I now use...
Expand|Select|Wrap|Line Numbers
  1. SELECT pHASH FROM cu_info.wc_search WHERE pUnixTimeStamp < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 700 DAY));
ps: The substring removal method was the way to go with the 2822 format datetime, but I didn't know how to do it in MySQL.
Oct 31 '11 #5
Rabbit
12,516 Expert Mod 8TB
So it sounds like you got it figured out?
Oct 31 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bruce A. Julseth | last post by:
My MySQL.user table (user, host, password) looks like the following: +---------+-----------+---------------------+ | user | host | password |...
6
by: Matt Liverance | last post by:
I REALLY dont want to switch to oracle :( but I cant get these tables working any faster. I've got 2 dedicated servers, each with a slave, all run 32gig 15k rpm raid 5 on u320 perc raid...
2
by: ms | last post by:
Access 2000: I am trying to delete duplicate records imported to a staging table leaving one of the duplicates to be imported into the live table. A unique record is based on a composite key of 3...
2
by: jkw | last post by:
Hi - I created a linked table via ODBC to a SQL Server database. The table in SQL Server contained over 12,000,000 records. Later, I started a "Delete" query. When I realized that I was deleting...
3
by: ebru | last post by:
about insert/delete/update records in mysql server with asp code how can I insert record in mysql database.. I insert into database but if I leave one text box empty, its give me error how can...
10
pezholio
by: pezholio | last post by:
Hi, I'm trying to put together a page that deletes records from a database based on if an item is unticked, I've got the existing items in an array (let's call it array1 for the sake of argument):...
5
kcdoell
by: kcdoell | last post by:
Hello: I am trying to write a code that will delete all records found in my DAO recordset Below is the code I have so far: 'Procdure to give the user the ability to delete all records 'for a...
6
by: Dilip1983 | last post by:
Hi All, I want to delete duplicate records from a large table. There is one index(INDEX_U1) on 4 columns(col1,col2,col3,col4) which is in unusable state. First of all when i tried to rebuild...
5
WyvsEyeView
by: WyvsEyeView | last post by:
Upon clicking Delete to delete the current record on frmTopics, I want several things to happen: 1) Display a custom message rather than Access's standard "You are about to delete n records" one....
2
by: ellenr | last post by:
I have a 600 record mysql db. It has a timestamp field, auto incr id field, several varchar fields and several smallint fields. The id field is set as primary key. The access03 has a form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.