
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL - TO_DAYS() Function
The DATE, DATETIME and TIMESTAMP datatypes in MySQL are used to store the date, date and time, time stamp values respectively. Where a time stamp is a numerical value representing the number of milliseconds from '1970-01-01 00:00:01' UTC (epoch) to the specified time. MySQL provides a set of functions to manipulate these values.
The MYSQL TO_DAYS() function accepts a date value as an argument and returns a numerical value representing the day number (the number of days since year 0).
Syntax
Following is the syntax of the above function –
TO_DAYS(date);
Where, date is the date value from which you need to get the day of the month.
Example 1
Following example demonstrates the usage of the TO_DAYS() function –
mysql> SELECT TO_DAYS('1898-03-22'); +-----------------------+ | TO_DAYS('1898-03-22') | +-----------------------+ | 693311 | +-----------------------+ 1 row in set (0.00 sec) mysql> SELECT TO_DAYS('2019-05-02'); +-----------------------+ | TO_DAYS('2019-05-02') | +-----------------------+ | 737546 | +-----------------------+ 1 row in set (0.00 sec)
Example 2
Following is another example of this function –
mysql> SELECT TO_DAYS('2021-11-29'); +-----------------------+ | TO_DAYS('2021-11-29') | +-----------------------+ | 738488 | +-----------------------+ 1 row in set (0.00 sec) mysql> SELECT TO_DAYS('1996-11-22'); +-----------------------+ | TO_DAYS('1996-11-22') | +-----------------------+ | 729350 | +-----------------------+ 1 row in set (0.00 sec)
Example 3
In the following example we are passing DATETIME value for date –
mysql> SELECT TO_DAYS('1987-3-12 12:35:58'); +-------------------------------+ | TO_DAYS('1987-3-12 12:35:58') | +-------------------------------+ | 725807 | +-------------------------------+ 1 row in set (0.00 sec) mysql> SELECT TO_DAYS('2018-05-23 20:40:32'); +--------------------------------+ | TO_DAYS('2018-05-23 20:40:32') | +--------------------------------+ | 737202 | +--------------------------------+ 1 row in set (0.00 sec)
Example 4
Following query converts the current time to days –
mysql> SELECT TO_DAYS(CURDATE()); +--------------------+ | TO_DAYS(CURDATE()) | +--------------------+ | 738356 | +--------------------+ 1 row in set (0.00 sec)
Example 5
We can also pass current timestamp values as arguments to this function –
mysql> SELECT TO_DAYS(CURRENT_TIMESTAMP()); +------------------------------+ | TO_DAYS(CURRENT_TIMESTAMP()) | +------------------------------+ | 738356 | +------------------------------+ 1 row in set (0.00 sec)
Example 6
You can also pass the column name as an argument to this function. Let us create a table with name MyPlayers in MySQL database using CREATE statement as shown below –
mysql> CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
Now, we will insert 7 records in MyPlayers table using INSERT statements −
mysql> insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); mysql> insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); mysql> insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); mysql> insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); mysql> insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); mysql> insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); mysql> insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
Following query converts the date of birth values of all the players into days and prints them –
mysql> SELECT First_Name, Last_Name, Date_Of_Birth, Country, TO_DAYS(Date_Of_Birth) as days FROM MyPlayers; +------------+------------+---------------+-------------+--------+ | First_Name | Last_Name | Date_Of_Birth | Country | days | +------------+------------+---------------+-------------+--------+ | Shikhar | Dhawan | 1981-12-05 | India | 723884 | | Jonathan | Trott | 1981-04-22 | SouthAfrica | 723657 | | Kumara | Sangakkara | 1977-10-27 | Srilanka | 722384 | | Virat | Kohli | 1988-11-05 | India | 726411 | | Rohit | Sharma | 1987-04-30 | India | 725856 | | Ravindra | Jadeja | 1988-12-06 | India | 726442 | | James | Anderson | 1982-06-30 | England | 724091 | +------------+------------+---------------+-------------+--------+ 7 rows in set (0.08 sec)
Example 7
Let us create another table with name Sales in MySQL database using CREATE statement as follows –
mysql> CREATE TABLE sales( ID INT, ProductName VARCHAR(255), CustomerName VARCHAR(255), DispatchDate date, DispatchTime time, Price INT, Location VARCHAR(255) ); Query OK, 0 rows affected (2.22 sec)
Now, we will insert 5 records in Sales table using INSERT statements −
insert into sales values (1, 'Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad'); insert into sales values (2, 'Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam'); insert into sales values (3, 'Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada'); insert into sales values (4, 'Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai'); insert into sales values (5, 'Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');
In the following query we are passing the column (name) DispatchDate as an argument of this function –
mysql> SELECT ProductName, CustomerName, DispatchDate, DispatchTime, Price, TO_DAYS(DispatchDate) as Days FROM sales; +-------------+--------------+--------------+--------------+-------+--------+ | ProductName | CustomerName | DispatchDate | DispatchTime | Price | Days | +-------------+--------------+--------------+--------------+-------+--------+ | Key-Board | Raja | 2019-09-01 | 11:00:00 | 7000 | 737668 | | Earphones | Roja | 2019-05-01 | 11:00:00 | 2000 | 737545 | | Mouse | Puja | 2019-03-01 | 10:59:59 | 3000 | 737484 | | Mobile | Vanaja | 2019-03-01 | 10:10:52 | 9000 | 737484 | | Headset | Jalaja | 2019-04-06 | 11:08:59 | 6000 | 737520 | +-------------+--------------+--------------+--------------+-------+--------+ 5 rows in set (0.00 sec)
Example 8
Suppose we have created a table named SubscriberDetails with 5 records in it using the following queries –
mysql> CREATE TABLE SubscriberDetails ( SubscriberName VARCHAR(255), PackageName VARCHAR(255), SubscriptionTimeStamp timestamp ); insert into SubscriberDetails values('Raja', 'Premium', TimeStamp('2020-10-21 20:53:49')); insert into SubscriberDetails values('Roja', 'Basic', TimeStamp('2020-11-26 10:13:19')); insert into SubscriberDetails values('Puja', 'Moderate', TimeStamp('2021-03-07 05:43:20')); insert into SubscriberDetails values('Vanaja', 'Basic', TimeStamp('2021-02-21 16:36:39')); insert into SubscriberDetails values('Jalaja', 'Premium', TimeStamp('2021-01-30 12:45:45'));
We can also pass the values of Timestamp as an argument of this function.
mysql> SELECT SubscriberName, PackageName, SubscriptionTimeStamp, TO_DAYS(SubscriptionTimeStamp) AS Days FROM SubscriberDetails; +----------------+-------------+-----------------------+--------+ | SubscriberName | PackageName | SubscriptionTimeStamp | Days | +----------------+-------------+-----------------------+--------+ | Raja | Premium | 2020-10-21 20:53:49 | 738084 | | Roja | Basic | 2020-11-26 10:13:19 | 738120 | | Puja | Moderate | 2021-03-07 05:43:20 | 738221 | | Vanaja | Basic | 2021-02-21 16:36:39 | 738207 | | Jalaja | Premium | 2021-01-30 12:45:45 | 738185 | +----------------+-------------+-----------------------+--------+ 5 rows in set (0.00 sec)