In SQLite, we can use the TIME()
function to add a given number of seconds to a time value.
If we’re dealing with datetime values, we can use the DATETIME()
function.
Example
Here’s an example that uses the TIME()
function:
SELECT TIME('22:50:10', '+1 second');
Result:
22:50:11
We can subtract the amount by replacing +
with -
.
Omitting +
results in the amount being added, as if we’d used +
:
SELECT TIME('22:50:10', '1 second');
Result:
22:50:11
We can specify the seconds in plural or non-plural form. In other words, second
is equivalent to seconds
:
SELECT
TIME('22:50:10', '20 second') AS second,
TIME('22:50:10', '20 seconds') AS seconds;
Result:
second seconds -------- -------- 22:50:30 22:50:30
The DATETIME()
Function
This example uses the DATETIME()
function to add fifteen seconds to a datetime value:
SELECT DATETIME('2050-08-21', '+15 seconds');
Result:
2050-08-21 00:00:15
In this case, I passed a date value and the function returned a datetime value with the number of seconds being added to 00:00:00.
Here’s an example with a datetime value being passed:
SELECT DATETIME('2050-08-21 23:59:59', '+47 seconds');
Result:
2050-08-22 00:00:46
In this case, the number of seconds being added was enough to push the date over to the next day.