Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Selecting how many "Days Old" a field is

Tags:

sql

mysql

How can I select how many days old a field is based on it's "DateAdded" field?

E.g.:

SELECT DAYSOLD(tbl_stuff.DateAdded) as DaysOld 
  FROM tbl_stuff
like image 325
Joe Avatar asked Jan 23 '23 09:01

Joe


2 Answers

Use:

SELECT DATEDIFF(NOW(), ts.dateadded) AS daysold
  FROM TBL_STUFF ts

Reference: DATEDIFF

DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other.

like image 179
OMG Ponies Avatar answered Feb 01 '23 08:02

OMG Ponies


You can use the DATEDIFF function, to get the difference, in days, between tbl_stuff.DateAdded and curdate().


For example :

mysql> select datediff(curdate(), '2010-03-15') as daysOld;
+---------+
| daysOld |
+---------+
|       8 |
+---------+
like image 33
Pascal MARTIN Avatar answered Feb 01 '23 09:02

Pascal MARTIN