Bipin Chandra Tripathi
Bipin Chandra Tripathi

Reputation: 2620

Mysql procedure to update difference between two dates for all records

I have a table with two dates and their difference

TABLE
Posting date     Expiry Date       Days Left
30-Mar-2014      30-April-2013      31
30-Mar-2014      30-April-2013      31
31-Mar-2014      30-April-2013      30

How can I create a procedure to update, calculate Days Left from current date (not posting date but the system date) to Expiry date for each record.

I am in doubt if I should store Days Left in database currently its facilitates user to sort record according to days left to expire.

Upvotes: 0

Views: 862

Answers (2)

Amnon
Amnon

Reputation: 2888

If I understood the question correctly, you want something of the following:

SELECT datediff(ExpiryDate,NOW()) AS E FROM MyTable ORDER BY E DESC

Upvotes: 1

bitfiddler
bitfiddler

Reputation: 2115

You should be able to use something like:

update tableName set DaysLeft = DATEDIFF(ExpiryDate, NOW())

You could put that in a stored proc, but it's just one line.

Upvotes: 1

Related Questions