Que el .NET te acompañe

Resistance is futile, you will be assimilated

Informix - Add month or year to date

Hola a tod@s,

Os dejo una función que he creado para evitar el error "The result of a datetime computation is out of range" que nos puede aparecer al intentar hacer una SQL en informix al tratar de sumar o restar meses o años a una fecha.

CREATE FUNCTION Add_datetime_interval
(dFecha    DATE,
 iIntervalo  int,
 bMeses  BOOLEAN,
 bRestar boolean) returning DATE as fecha

   DEFINE dResult  DATE;       
   DEFINE iDay           INT;        
   DEFINE iMonth         INT;         

  LET iDay = DAY(dFecha);
  LET dResult = dFecha - (iDay - 1) UNITS DAY;
  LET iMonth = MOD(MONTH(dFecha) + iIntervalo, 12) ; 

  IF NOT bMeses THEN
    IF NOT bRestar THEN
      LET dResult = dResult + iIntervalo UNITS YEAR; 
    ELSE
      LET dResult = dResult - iIntervalo UNITS YEAR;
    END IF
    LET iMonth = MONTH(dResult + (iDay - 1) UNITS DAY);        
  ELSE
    IF NOT bRestar THEN
      LET dResult = dResult + iIntervalo UNITS MONTH;      
    ELSE
      LET dResult = dResult - iIntervalo UNITS MONTH;
    END IF
    LET iMonth = MONTH(dResult + (iDay - 1) UNITS DAY);
  END IF 

  IF iMonth <> MONTH(dResult) THEN
    LET dResult = dResult + (iDay - 1) UNITS DAY;
    LET iDay = DAY(dResult);
    LET dResult = dResult - iDay UNITS DAY;
  ELSE
    LET dResult = dResult + (iDay - 1) UNITS DAY;
  END IF 

  RETURN dResult;

END FUNCTION;

Espero que os ayude como a mi me ha servido.

Saludos.

Posted: 24/7/2009 13:10 por Carmen Sanchez | con 4 comment(s)
Archivado en:
Comparte este post:

Comentarios

Edward Ocando ha opinado:

Por Dios Carmen, esta muy bueno. Te amo, aunque estés lejos, te amo. Eres lo máximo.. Besos desde Venezuela.

# October 13, 2009 6:36 AM

Juan ha opinado:

Hola, Carmen es muy interesante esta nota, estoy tratando de llamar a un 4gi a traves de "fglgo", desde un "store" o una "function" sobre "informix" con el comando "system" y no logro hacerlo.

Tenes alguna idea de como hacer esto o si ya lo has intentado antes.

De todos modos gracias.

Saludos,

Juan

# November 4, 2009 5:08 PM

custom thesis ha opinado:

If you are willing to enlarge your knowledge referring to this good post, look for thesis writing or custom dissertation and order the best the dissertation there.

# March 16, 2011 2:22 PM