Im validating an csv file that is being imported to the database via LOAD DATA LOCAL INFILE
and im using a before insert trigger
to achieve that (it is a dirty table, so when this process finishes i bulk correct data to another table). The problem is that i have a DATE field (Y-m-d) and if the script reads a non-existent date (ex: 2023-09-31, 2023-02-30) it throws:
Invalid datetime format: 1292 Truncated incorrect date value: ‘2023-09-31’.
I’ve trying to *catch *the error within my trigger but it is always showing the same error in my frontend.
heres part of my trigger.
CREATE DEFINER=`root`@`localhost` TRIGGER `dirty_si_base_AFTER_INSERT` AFTER INSERT ON `dirty_si_base_AFTER_INSERT` FOR EACH ROW BEGIN
[...]
DECLARE fecha_valida_notificacion BOOLEAN;
[...]
-- Verificamos si la fecha fallo es válida
SET fecha_valida_notificacion = (
STR_TO_DATE(NEW.fecha_fallo_notificacion, '%Y-%m-%d') IS NOT NULL AND
STR_TO_DATE(NEW.fecha_fallo_notificacion, '%Y-%m-%d') = NEW.fecha_fallo_notificacion
);
-- Si la fecha no es válida, ingresamos a la tabla de errores
IF NOT fecha_valida_notificacion THEN
insert into errores_carga(con, mensaje, campo, archivo, created_at)
values (cast(new.id as char), 'La fecha no existe', 5, 'SI', now());
END IF;
END
Im expecting to handle this error and insert a message in another table just like other errors.
2
Answers
Try:
Use a Temporary Variable to Parse the Date:
Instead of directly comparing the parsed date to the original date string, you can use a temporary variable to hold the parsed date and then compare it.
Use
NULLIF
andSTR_TO_DATE
Together:NULLIF
can be used to returnNULL
if the date string does not match the expected format.My updated version:
STR_TO_DATE
In short:
STR_TO_DATE
considers2023-02-31
as well as2023-09-31
to be valid.But since (at least) your format is already valid, you can use
DATE()
instead, which will return
NULL
for2023-02-31
and2023-09-31
.