alter session set nls_date_format="DD/MM/RRRR hh24:mi:ss";

declare 
	formato varchar2(24);
	
    cursor c_error is 
    SELECT DISTINCT
      NVL(TRIM(SOL.FECHARECEPCION_000), SYSDATE) AS F_RECEPCION_00,       -- no me dice de donde sacarlo y no puede ser null FECHA DE RECEPCIÓN DE LA SOLICITUD
      NVL(TRIM(SOL.FECHAREGOTRO_000), null)   AS F_REGISTRO_OTRO_00, 
	  NVL(TRIM(SOL.FECHAREGDPTO_000), SYSDATE)   AS F_REGISTRO_00,
      VALOR_AD74A
      FROM X87B.X8700T00@X87B_DBL0 SOL, (SELECT cl.VALOR_X87B ID_SOLICITANTE_87, VALOR_AD74A ID_SOLICITANTE_01, ID_PERFIL_01,IDIOMA_SERVICIO_01, ID_DIRECCION_01, ID_CUENTA_01 ID_CUENTA_05,  
                                                FECHAALTA_009 FECHAALTA_009, DECODE(IDTIPOMOTSOL_009,1,2,2,3,3,4,4,5,5,8,6,6,7,7) IDTIPOMOTSOL
                                            FROM CLAVES_AD7401 CL, AD74A01T00, X87B.X8709T00@X87B_DBL0 X09
                                            WHERE VALOR_AD74A = ID_SOLICITANTE_01
                                            AND VALOR_X87B = X09.IDSOLICITANTE_001
                                            AND USU_ALTA_01 = 'MIGRADO') T01, 
         X87B.X8701T00@X87B_DBL0 TE, CLAVES_AD7400, X87B.X8735T00@X87B_DBL0 DA
     WHERE SOL.IDSOLICITANTE_001 IS NOT NULL
     AND SOL.IDSOLICITANTE_001 = T01.ID_SOLICITANTE_87(+)
     AND SOL.IDSOLICITANTE_001 = TE.IDSOLICITANTE_001(+)
	 AND TE.IDCONTACTONOTIFI_035 = DA.IDCONTACTO_035(+)
	 AND SOL.IDORIGENSOLI_006 IS NOT NULL
	 AND TRIM(SOL.IDSOLICITUD_000)  = VALOR_X87B(+)
     AND NVL(TRIM(SOL.FECHAREGDPTO_000), SYSDATE) < TO_DATE('01/01/2001', 'DD/MM/RRRR');
     --and VALOR_AD74A = 99200;
begin

    select value into formato from nls_session_parameters where parameter = 'NLS_DATE_FORMAT'; 
    
    for r1 in c_error loop
        dbms_output.put_line(r1.F_RECEPCION_00);
        dbms_output.put_line(r1.F_REGISTRO_00);
        INSERT INTO ERROR_FECHA VALUES (r1.VALOR_AD74A,r1.F_RECEPCION_00,r1.F_REGISTRO_00,formato);
        COMMIT;
    end loop;
end;

/

COMMIT;

/