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 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 2
In this example, we are using NOW, CURDATE, along with some additional operations 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');
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);
The 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);