w3resource

MySQL TO_DAYS() function

TO_DAYS() function

MySQL TO_DAYS() returns a number of days between a given date and year 0.

This function is useful in -

  • TO_DAYS() can be used to calculate ages or durations in days, which can be useful for tasks like determining the age of a person based on their birthdate.
  • It's valuable for sorting records or data based on dates in ascending or descending order of days since year 0.
  • It's used in time series analysis to perform calculations based on the numeric representation of dates.
  • TO_DAYS() enables you to calculate date ranges based on the numeric representation of dates.
  • TO_DAYS() supports the analysis of historical data by providing a consistent numeric representation of dates.
  • The function is useful for filtering records or data based on date criteria, such as selecting records within a specific date range.

Syntax:

TO_DAYS(date);

Where date is a date.

Syntax Diagram:

MySQL TO_DAYS() Function - Syntax Diagram

MySQL Version: 8.0


Pictorial Presentation:

Pictorial Presentation of MySQL TO_DAYS() function

Example:

The following statement will return the number of days from year 0 to 2009-05-18.

Code:

SELECT  TO_DAYS('2009-05-18');

Output:

mysql> SELECT  TO_DAYS('2009-05-18');
+-----------------------+
| TO_DAYS('2009-05-18') |
+-----------------------+
|                733910 | 
+-----------------------+
1 row in set (0.03 sec)

Example: TO_DAYS() function using numeric format

The following statement will return the number of days from year 0 to 20090518.

Code:

SELECT  TO_DAYS(20090518);

Output:

mysql> SELECT  TO_DAYS(20090518);
+-------------------+
| TO_DAYS(20090518) |
+-------------------+
|            733910 | 
+-------------------+
1 row in set (0.00 sec)

Video Presentation:

All Date and Time Functions:

Click here to see the MySQL Date and time functions.

Previous: TIMESTAMPDIFF()
Next: UNIX_TIMESTAMP()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql/date-and-time-functions/mysql-to_days-function.php