SQL >> Base de Datos >  >> RDS >> SQLite

Contar los segundos transcurridos desde cualquier fecha u hora en SQLite

Cuando necesitas el número de segundos transcurridos desde una fecha y hora concretas, la función que debes llamar es UNIXEPOCH().

Una advertencia: apareció en SQLite 3.38.0, así que necesitarás esa versión o una posterior.

Ejemplo

SELECT UNIXEPOCH() - UNIXEPOCH('2020-02-23 07:30:45');

Resultado:

64254554

Eso son los segundos transcurridos desde 2020-02-23 07:30:45.

Ejecútalo más tarde y la cifra, naturalmente, crece:

SELECT UNIXEPOCH() - UNIXEPOCH('2020-02-23 07:30:45');

Resultado:

64254823

Segundos desde el inicio del mes

Aquí está el tiempo transcurrido desde que empezó el mes actual:

SELECT UNIXEPOCH() - UNIXEPOCH(DATETIME('now', 'start of month'));

Resultado:

605380

DATETIME() con el argumento now proporciona el momento actual, y el modificador start of month retrocede hasta el primer día del mes.

Para ver las propias fechas junto al intervalo entre ellas:

SELECT DATETIME('now', 'start of month') AS "Start of Month", DATETIME('now') AS "Now", UNIXEPOCH() - UNIXEPOCH(DATETIME('now', 'start of month')) AS "Seconds";

Resultado:

Start of Month Now Seconds ------------------- ------------------- ------- 2022-03-01 00:00:00 2022-03-08 00:12:04 605524 

Sustituye por start of day o start of year y obtendrás los segundos desde el comienzo de hoy o del año.