Jump to content

Diffrence in days in two dates


natasha_sharma
Go to solution Solved by Barand,

Recommended Posts

Hi,

 

I want to update a column in my table with the difference between two dates. resultdate  variable is in yyyy-mm-dd

update table test_table set days_diff = (resultdate - CURDATE()) where 1 = 1;

So, i want to reset my days_diff variable with the latest date when the code is run.

 

But this code is given me correct days difference in first run, but when i instantly run this code, I get wrong number of differences.

 

Is there any better code to to do this ?

Edited by natasha_sharma
Link to comment
Share on other sites

  • Solution

You need to use the DATEDIFF() function

 

http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_datediff

 

but the best way to do it is not to do it at all.

 

Derived data should not be stored in your database. Calculate it when required instead of continually updating the table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.