25/10/21 16:54 How to Query Date and Time in MySQL - PopSQL
Presentamos PopSQL + dbt. El primer editor de SQL con soporte dbt incorporado. Leer más →
Sign In
Aprenda SQL / MySQL / Cómo consultar la fecha y la hora en MySQL
Cómo consultar la fecha y la hora en
MySQL
MySQL tiene las siguientes funciones para obtener la fecha y hora actual:
SELECT now(); -- date and time
SELECT curdate(); --date
SELECT curtime(); --time in 24-hour format
Para buscar filas entre dos fechas o marcas de tiempo:
SELECT *
FROM events
where event_date between '2018-01-01' and '2018-01-31';
-- Can include time by specifying in YYYY-MM-DD hh:mm:ss format:
SELECT *
FROM events
WHERE event_date BETWEEN '2018-01-01 12:00:00' AND '2018-01-01 23:30:00';
Para buscar filas creadas durante la última semana:
SELECT *
FROM events
WHERE event_date > date_sub(now(), interval 1 week);
https://popsql.com/learn-sql/mysql/how-to-query-date-and-time-in-mysql 1/4
25/10/21 16:54 How to Query Date and Time in MySQL - PopSQL
También hay DATE_ADD() . Por ejemplo, para buscar eventos programados entre hace una
semana y 3 días a partir de ahora:
SELECT *
FROM events
WHERE event_date BETWEEN date_sub(now(), interval 1 week) AND date_add(now(), interval
Puede extraer parte de una marca de tiempo aplicando la función correspondiente:
SELECT year(now()); -- or month(), day(), hour(), minute(), second()
Para obtener un día de la semana a partir de una marca de tiempo, use la
DAYOFWEEK() función:
-- returns 1-7 (integer), where 1 is Sunday and 7 is Saturday
SELECT dayofweek('2018-12-12');
-- returns the string day name like Monday, Tuesday, etc
SELECT dayname(now());
Para convertir una marca de tiempo en una marca de tiempo Unix (segundos enteros):
-- This will assume time to be 12am
SELECT unix_timestamp('2018-12-09');
-- You can specify an exact timestamp to be converted down to the second
SELECT unix_timestamp('2018-12-09 14:53:21');
-- calling unix_timestamp without a parameter will be like calling it for current time
SELECT unix_timestamp(); -- same as SELECT unix_timestamp(now());
https://popsql.com/learn-sql/mysql/how-to-query-date-and-time-in-mysql 2/4
25/10/21 16:54 How to Query Date and Time in MySQL - PopSQL
Para calcular la diferencia entre dos marcas de tiempo, conviértalas en marcas de tiempo
Unix y luego realice la resta:
-- show seconds between delivery and shipping timestamps
SELECT unix_timestamp(delivered_at) - unix_timestamp(shipped_at);
FROM deliveries;
-- convert computed difference to hh:mm:ss format:
SELECT sec_to_time(unix_timestamp(delivered_at) - unix_timestamp(shipped_at))
FROM deliveries;
Tenga en cuenta que MySQL también tiene funciones DATEDIFF() y TIMEDIFF() , pero
solo se pueden usar con valores puramente de fecha o valores puramente de tiempo,
respectivamente.
Difundir la palabra Pío
Anterior próximo
Cómo Alterar La Secuencia Cómo Agrupar Por Tiempo
¿Listo para un editor SQL moderno?
Empiece gratis
https://popsql.com/learn-sql/mysql/how-to-query-date-and-time-in-mysql 3/4
25/10/21 16:54 How to Query Date and Time in MySQL - PopSQL
Sobre Trabajos Seguridad Términos de servicio Política de privacidad
© PopSQL, Inc. 2021
https://popsql.com/learn-sql/mysql/how-to-query-date-and-time-in-mysql 4/4