En MySQL, puede usar DATE_ADD()
función para agregar una cantidad específica de tiempo a una fecha. Por ejemplo, podría usarlo para agregar 5 días a una fecha determinada. Puede especificar si desea agregar días, semanas, meses, trimestres, años, etc. También puede agregar un valor de tiempo, como segundos, microsegundos, etc.
Este artículo contiene ejemplos que demuestran cómo DATE_ADD()
la función funciona.
Sintaxis
La sintaxis es así:
DATE_ADD(date,INTERVAL expr unit)
Ejemplo 1:uso básico
Aquí hay un ejemplo de uso.
SELECT DATE_ADD('2020-10-01', INTERVAL 20 DAY) AS Result;
Resultado:
+------------+ | Result | +------------+ | 2020-10-21 | +------------+
Este ejemplo agrega 20 días a la fecha proporcionada por el primer argumento.
Ejemplo 2:otras unidades de fecha
Puede especificar las unidades en días, semanas, meses, años, etc. Aquí hay algunos ejemplos.
SELECT '2020-10-01' AS 'Start Date', DATE_ADD('2020-10-01', INTERVAL 2 WEEK) AS '+2 Weeks', DATE_ADD('2020-10-01', INTERVAL 2 MONTH) AS '+2 Months', DATE_ADD('2020-10-01', INTERVAL 2 QUARTER) AS '+2 Quarters', DATE_ADD('2020-10-01', INTERVAL 2 YEAR) AS '+2 Years';
Resultado:
+------------+------------+------------+-------------+------------+ | Start Date | +2 Weeks | +2 Months | +2 Quarters | +2 Years | +------------+------------+------------+-------------+------------+ | 2020-10-01 | 2020-10-15 | 2020-12-01 | 2021-04-01 | 2022-10-01 | +------------+------------+------------+-------------+------------+
Ejemplo 3:Unidades de tiempo
También puede agregar unidades de tiempo a un valor de fecha/hora. He aquí un ejemplo.
SELECT DATE_ADD('2020-10-01 01:00:00', INTERVAL 10 HOUR) AS Result;
Resultado:
+---------------------+ | Result | +---------------------+ | 2020-10-01 11:00:00 | +---------------------+
Y puede especificar varias unidades al mismo tiempo. Por ejemplo, puede especificar horas y minutos. Así.
SELECT DATE_ADD('2020-10-01 01:00:00', INTERVAL '1:30' HOUR_MINUTE) AS Result;
Resultado:
+---------------------+ | Result | +---------------------+ | 2020-10-01 02:30:00 | +---------------------+
Valores esperados
La siguiente tabla muestra los valores unitarios válidos y su formato esperado.
unit Valor | Esperado expr Formato |
---|---|
MICROSEGUNDO | MICROSEGUNDOS |
SEGUNDO | SEGUNDOS |
MINUTO | MINUTOS |
HORA | HORAS |
DÍA | DÍAS |
SEMANA | SEMANAS |
MES | MESES |
TRIMESTRE | TRIMESTRE |
AÑO | AÑOS |
SEGUNDO_MICROSEGUNDO | ‘SEGUNDOS.MICROSEGUNDOS’ |
MINUTO_MICROSEGUNDO | ‘MINUTOS:SEGUNDOS.MICROSEGUNDOS’ |
MINUTO_SEGUNDO | 'MINUTOS:SEGUNDOS' |
HORA_MICROSEGUNDO | ‘HORAS:MINUTOS:SEGUNDOS.MICROSEGUNDOS’ |
HORA_SEGUNDO | ‘HORAS:MINUTOS:SEGUNDOS’ |
HORA_MINUTO | ‘HORAS:MINUTOS’ |
DÍA_MICROSEGUNDO | ‘DÍAS HORAS:MINUTOS:SEGUNDOS.MICROSEGUNDOS’ |
DÍA_SEGUNDO | ‘DÍAS HORAS:MINUTOS:SEGUNDOS’ |
DÍA_MINUTO | ‘DÍAS HORAS:MINUTOS’ |
DÍA_HORA | ‘DÍAS HORAS’ |
AÑO_MES | 'AÑOS-MESES' |
También puede usar ADDDATE()
función para hacer lo mismo (es un sinónimo de DATE_ADD()
función cuando se usa la misma sintaxis). Además, la sintaxis de ADDDATE()
tiene una segunda forma, que es un método abreviado para agregar una cierta cantidad de días a una fecha. Para obtener más información, consulte Ejemplos de ADDDATE() en MySQL.