©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The TIME_TO_SEC SQL Function in MySQL and MariaDB - Convert time to seconds

TIME_TO_SECSyntax:TIME_TO_SEC(Time)
Return value:INTEGER, DECIMAL
Function type:Date and Time function
Function Description

The TIME_TO_SEC() function converts the "Time" argument to its number of seconds.

The "Time" argument must be of type TIME or DATETIME.

If the argument is of the DATETIME type, the time portion is used for the calculation.

If the argument is of type DATE, the function returns 0.

If the argument is a string containing only a date, the function returns an incorrect value.

Microseconds contained in the argument are returned as a decimal with.

If the value for "Time" is invalid or NULL, the TIME_TO_SEC() function returns NULL.

SQL Examples for the TIME_TO_SEC function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20

SELECT time_to_sec(now());
SELECT time_to_sec(utc_timestamp());

SELECT time_to_sec('2022-03-29');          /* wrong result */
SELECT time_to_sec('2022-03-29 12:15:30');
SELECT time_to_sec('12:15:30');

SELECT time_to_sec(20210216);
SELECT time_to_sec(20210216121530);
SELECT time_to_sec(121530);

SELECT time_to_sec('12:15:30.099');
SELECT time_to_sec(1381530.099);

SELECT time_to_sec('0000-00-00 00:00:00');
SELECT time_to_sec(unix_timestamp());
SELECT time_to_sec('xxx');
SELECT time_to_sec(null);

time_to_sec(now())
bigint(17)
51182
time_to_sec(utc_timestamp())
bigint(17)
43982
time_to_sec('2022-03-29')
bigint(17)
1222
time_to_sec('2022-03-29 12:15:30')
bigint(17)
44130
time_to_sec('12:15:30')
bigint(17)
44130
time_to_sec(20210216)
bigint(17)
0
time_to_sec(20210216121530)
bigint(17)
44130
time_to_sec(121530)
bigint(17)
44130
time_to_sec('12:15:30.099')
decimal(21)
44130.099
time_to_sec(1381530.099)
decimal(21)
497730.099
time_to_sec('0000-00-00 00:00:00')
bigint(17)
0
time_to_sec(unix_timestamp())
bigint(17)
NULL
time_to_sec('xxx')
decimal(24)
NULL
time_to_sec(null)
bigint(17)
NULL
The examples were created with the MyWAY SQL manager: Download

How to use the TIME_TO_SEC() function in MySQL and MariaDB databases

In MySQL and MariaDB the TIME_TO_SEC() function converts a time value to seconds by using a time expression takes as an argument and returns the total number of seconds contained in that time. Column names from tables or other expressions can also be used as parameters in order to carry out calculations or comparisons on the basis of time intervals represented in seconds. The feature allows time values ​​to be more easily manipulated and worked with, considering the time value as a duration rather than a specific point in time and ignoring any date components. This facilitates time-based calculations and operations in SQL queries, SQL scripts and data manipulations in the database.
TANTIMETIME_FORMATTIME_TO_SECTIMEDIFFTIMESTAMPTIMESTAMPADD

Further MySQL und MariaDB SQL Date and Time functions

ADDDATEADDDATE(Date, [INTERVAL] Number [Unit])
More about ADDDATE Function

ADDTIMEADDTIME(Date, Expression)
More about ADDTIME Function

CONVERT_TZCONVERT_TZ(Date, From time zone, To time zone)
More about CONVERT_TZ Function

CURDATECURDATE()
More about CURDATE Function

CURTIMECURTIME([Precision])
More about CURTIME Function

CURRENT_DATECURRENT_DATE(-)
More about CURRENT_DATE Function

CURRENT_TIMECURRENT_TIME([Precision])
More about CURRENT_TIME Function

CURRENT_TIMESTAMPCURRENT_TIMESTAMP([Precision])
More about CURRENT_TIMESTAMP Function

DATEDATE(Date and Time)
More about DATE Function

DATE_ADDDATE_ADD(Date, INTERVAL Number Unit)
More about DATE_ADD Function

DATE_SUBDATE_SUB(Date, INTERVAL Number Unit)
More about DATE_SUB Function

FROM_DAYSFROM_DAYS(Number of days)
More about FROM_DAYS Function

FROM_UNIXTIMEFROM_UNIXTIME(Unix timestamp [, Format])
More about FROM_UNIXTIME Function

LOCALTIMELOCALTIME([Precision])
More about LOCALTIME Function

LOCALTIMESTAMPLOCALTIMESTAMP([Precision])
More about LOCALTIMESTAMP Function

MAKEDATEMAKEDATE(Year, Day of year)
More about MAKEDATE Function

MAKETIMEMAKETIME(Hours, Minutes, Seconds)
More about MAKETIME Function

NOWNOW([Precision])
More about NOW Function

SEC_TO_TIMESEC_TO_TIME(Seconds)
More about SEC_TO_TIME Function

SLEEPSLEEP(Seconds)
More about SLEEP Function

STR_TO_DATESTR_TO_DATE(String, Format)
More about STR_TO_DATE Function

SUBDATESUBDATE(Date, [INTERVAL] Number [Unit])
More about SUBDATE Function

SUBTIMESUBTIME(Date and Time, Deduction)
More about SUBTIME Function

SYSDATESYSDATE()
More about SYSDATE Function

TIMETIME(Date and Time)
More about TIME Function

TIMEDIFFTIMEDIFF(Date 1, Date 2)
More about TIMEDIFF Function

TIMESTAMPTIMESTAMP(Date [, Addition])
More about TIMESTAMP Function

TIMESTAMPADDTIMESTAMPADD(Unit, Number, Date)
More about TIMESTAMPADD Function

TIMESTAMPDIFFTIMESTAMPDIFF(Unit, Date 1, Date 2)
More about TIMESTAMPDIFF Function

UTC_DATEUTC_DATE()
More about UTC_DATE Function

UTC_TIMEUTC_TIME([Precision])
More about UTC_TIME Function

UTC_TIMESTAMPUTC_TIMESTAMP([Precision])
More about UTC_TIMESTAMP Function

DAYDAY(Date)
More about DAY Function

DAYOFMONTHDAYOFMONTH(Date)
More about DAYOFMONTH Function

DAYOFWEEKDAYOFWEEK(Date)
More about DAYOFWEEK Function

DAYOFYEARDAYOFYEAR(Date)
More about DAYOFYEAR Function

EXTRACTEXTRACT(Unit FROM Date)
More about EXTRACT Function

HOURHOUR(Date and Time)
More about HOUR Function

MICROSECONDMICROSECOND(Date and Time)
More about MICROSECOND Function

MINUTEMINUTE(Date and Time)
More about MINUTE Function

MONTHMONTH(Date)
More about MONTH Function

QUARTERQUARTER(Date)
More about QUARTER Function

SECONDSECOND(Date and Time)
More about SECOND Function

TO_DAYSTO_DAYS(Date)
More about TO_DAYS Function

TO_SECONDSTO_SECONDS(Date and Time)
More about TO_SECONDS Function

UNIX_TIMESTAMPUNIX_TIMESTAMP([Date and Time])
More about UNIX_TIMESTAMP Function

WEEKWEEK(Date [, Mode])
More about WEEK Function

WEEKDAYWEEKDAY(Date)
More about WEEKDAY Function

WEEKOFYEARWEEKOFYEAR(Date)
More about WEEKOFYEAR Function

YEARYEAR(Date)
More about YEAR Function

YEARWEEKYEARWEEK(Date [, Mode])
More about YEARWEEK Function

DAYNAMEDAYNAME(Date)
More about DAYNAME Function

DATE_FORMATDATE_FORMAT(Date and Time, Format)
More about DATE_FORMAT Function

GET_FORMATGET_FORMAT(Type, Format)
More about GET_FORMAT Function

MONTHNAMEMONTHNAME(Date)
More about MONTHNAME Function

TIME_FORMATTIME_FORMAT(Time, Format)
More about TIME_FORMAT Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the TIME_TO_SEC SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer