Days until TODAY in SQL

How to calculate the number of days till today starting from a field date or datetime – in this case created_date.table :

1. My first version:

@SELECT ROUND((UNIX_TIMESTAMP()-(UNIX_TIMESTAMP(created_date)))/86400) AS age FROM table@

Actually I remember seeing one time my friend and Java programmer Razvan Stroescu doing it. I am glad I can do it now also.

2. The second way is even simpler. Thanks to Manel Vaquero, my colleague now.

@SELECT TO_DAYS(CURRENT_DATE) - TO_DAYS(created_date)@

3. Yet another way

@SELECT DATEDIFF(NOW(),created_date) AS days@

SQL is a very nice thing to learn. Enjoy it as I do


About this entry