MySQL TO_DAYS Function

MySQL TO_DAYS function is one of the MySQL 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 MySQL To days function. 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');
MySQL TO_DAYS Function 1

MySQL TO_DAYS Example 2

In this example, we are using NOWCURDATE, 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 example of the MySQL To_days function. This time, we are using the YYMMDD format as the date argument. From the below Date Function example, 19 means 2019

SELECT TO_DAYS(730529);

SELECT TO_DAYS(191231);

SELECT TO_DAYS(651231);
MySQL TO_DAYS Function 3

MySQL To days function will throw an error if it finds the Wrong Date Or Zero date. This example shows you the same. Within the third statement, we are using the YYMMDD date, 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