MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL TO_SECONDS() Function



The MySQL TO_SECONDS() function returns the date or datetime value converted to seconds. This function returns the number of seconds between a given date/datetime value and year 0. It returns NULL if datetime is 0.

This function is to be used only with dates within the Gregorian calendar.

Syntax

TO_SECONDS(datetime)

Parameters

datetime Required. Specify a date or datetime value to convert to seconds.

Return Value

Returns the converted seconds.

Example 1:

The example below shows the usage of TO_SECONDS() function.

mysql> SELECT TO_SECONDS('2018-08-18');
Result: 63701769600

mysql> SELECT TO_SECONDS('2018-08-18 10:38:42');
Result: 63701807922

mysql> SELECT TO_SECONDS('2018-08-18 10:38:42.000004');
Result: 63701807922

mysql> SELECT TO_SECONDS(20180818);
Result: 63701769600

mysql> SELECT TO_SECONDS('0000-01-01');
Result: 86400

mysql> SELECT TO_SECONDS('0000-00-00');
Result: NULL

Example 2:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeDate_of_Joining
1JohnLondon252018-05-25
2MarryNew York242018-10-15
3JoParis272019-06-09
4KimAmsterdam302019-09-21
5RameshNew Delhi282019-10-25
6SureshMumbai282021-12-26

The statement given below can be used to convert the records of column Date_of_Joining into seconds:

SELECT *, TO_SECONDS(Date_of_Joining) AS TO_SECONDS_Value FROM Employee;

This will produce the result as shown below:

EmpIDNameCityAgeDate_of_JoiningTO_SECONDS_Value
1JohnLondon252018-05-2563694425600
2MarryNew York242018-10-1563706780800
3JoParis272019-06-0963727257600
4KimAmsterdam302019-09-2163736243200
5RameshNew Delhi282019-10-2563739180800
6SureshMumbai282021-12-2663807696000

❮ MySQL Functions