MySQL TO_DAYS Function

MySQL TO_DAYS is one of the Date Functions, which returns the total number of days from year 0 to the user given date. The syntax of the TO_DAYS Function in MySQL is as shown below:

TO_DAYS(date);

MySQL TO_DAYS function Example

The following examples help you understand the use of the To days method. Here, we are returning the total number of days from 0 to different dates.

SELECT TO_DAYS('1900-01-01');

SELECT TO_DAYS('2007-12-31');

SELECT TO_DAYS('2017-12-31');
TO DAYS Example 1

TO DAYS Example 2

In this example, we are using NOW, CURDATE, along with some addition operation inside this TO_DAYS function. It means we are using the MySQL Date value in String format.

SELECT TO_DAYS(NOW());

SELECT TO_DAYS(CURDATE()), TO_DAYS(CURDATE() + 2);

SELECT TO_DAYS('20171231');
MySQL TO_DAYS Function 2

Let us see another query of it and this time, we are using the YYMMDD format as the argument. From the below Date method example, 19 means 2019

SELECT TO_DAYS(730529);

SELECT TO_DAYS(191231);

SELECT TO_DAYS(651231);
MySQL TO_DAYS Function 3

The MySQL TO_DAY function will throw an error if it finds the Wrong Date Or Zero. This example shows you the same. Within the third statement, we are using the YYMMDD, and we know 36 days is the wrong value.

SELECT TO_DAYS('0000-00-00');

SELECT TO_DAYS('2018-01-59');

SELECT TO_DAYS(351236);
MySQL TO_DAYS Function 4