Link to home
Start Free TrialLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

calculate date difference

Hi,

I need to delete all email that are more than 30 days.

Delete from Email
where datediff(NOW(),PostDate) > 30

any idea why my query is not deleting?


Thanks,
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

I would go this way, to enhance performance:
Delete from Email
where PostDate < NOW() - 30


apart from that: is your field PostDate defined as datetime or as text field?
Avatar of lulu50

ASKER

angelIII:

I am using MySql

I am having problem with the date getting inserted  then delete all emails more than 30 days.

Please, tell me what I am doing wrong I cannot insert the date to do the 30 days different

the field is called PostDate

            INSERT INTO `lulu50_ChurchesBulletin`.`Email` (
                  `UserID`,
                  `UserName`,
                  `From`,
                  `Subject`,
                  `PostDate`,
                  `Body`,
                  `EmailStatus`
                  )
                  VALUES (
                  '#session.AdsUserID#', '#cookie.MemUser#', '#Trim(FORM.TxtFrom)#',
                  '#Trim(FORM.TxtSubject)#', 'now()', '#Trim(FORM.input)#','i');
Avatar of lulu50

ASKER

angelIII:

I am able to insert right now

I removed 'Now()'  to Now()
Avatar of lulu50

ASKER

angelIII:

Delete from Email
where PostDate < NOW() - 30

it is deleting all my emails
Avatar of lulu50

ASKER

PostDate is defined as date
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lulu50

ASKER

I set the date value to my record to be 2011-12-29

but I am still seeing the record.

the type for PostDate is Date
Avatar of lulu50

ASKER

How DATE_SUB(NOW() display the date?

my field display the date like this 2011-12-29
2011-12-29 is in the future, not in the past.
I understand you want to delete data older than 30 days?
Avatar of lulu50

ASKER

yes I do want it to be in the future


ok, please help me explain:

a user comes to my site sends an email I take the date when the user sends the email calculated to what every the current date and if the difference more than 30 days I want to delete their email.
not clearer to me ...

let's speak with data samples:
it's today 2011-10-28.
I am your customer, I want to send an email with your site.
the email is stored with today's date?

so, 30 days later (2011-11-27), you want this email to be deleted, right?
so, you have to consider yourself to be on that date, so there you want to delete emails that are 30 days old, aka created 30 days before "now()"

so, my suggestion should work.
Avatar of lulu50

ASKER

I got it to work !!!!!


Thank you
Avatar of lulu50

ASKER

Thank you