The dtsub() function
The dtsub() function subtracts one datetime value from another. The result is an interval value.
Syntax
mint dtsub(d1, d2, inv)
dtime_t *d1, *d2;
intrvl_t *inv;
- d1
- A pointer to an initialized datetime host variable.
- d2
- A pointer to an initialized datetime host variable.
- inv
- A pointer to the interval host variable that contains the result.
Usage
The dtsub() function subtracts the datetime value d2 from d1 and stores the interval result in inv. The result can be either a positive or a negative value. If necessary, the function extends d2 to match the qualifier for d1, before the subtraction.
Initialize the qualifier for inv with a value in either the year to month or day to fraction(5) classes. When d1 contains fields in the day to fraction class, the interval qualifier must also be in the day to fraction class.
Return codes
- 0
- The subtraction was successful.
- <0
- An error occurred while performing the subtraction.
Example
The demo directory
contains this sample program in the file dtsub.ec.
The program performs datetime subtraction that returns equivalent interval results
in the range of year to month and month to month and
attempts to return an interval result in the range day to
hour.
/*
* dtsub.ec *
The following program subtracts one DATETIME value from another and
displays the resulting INTERVAL value or an error message.
*/
#include <stdio.h>
EXEC SQL include datetime;
main()
{
mint x;
char out_str[16];
EXEC SQL BEGIN DECLARE SECTION;
datetime year to month dt_var1, dt_var2;
interval year to month i_ytm;
interval month to month i_mtm;
interval day to hour i_dth;
EXEC SQL END DECLARE SECTION;
printf("DTSUB Sample ESQL Program running.\n\n");
printf("Datetime (year to month) value #1 = 2007-10\n");
dtcvasc("2007-10", &dt_var1);
printf("Datetime (year to month) value #2 = 2001-08\n");
dtcvasc("2001-08", &dt_var2);
printf("-----------------------------------------------------------\n");
/* Determine year-to-month difference */
printf("Difference (year to month) = ");
if(x = dtsub(&dt_var1, &dt_var2, &i_ytm))
printf("Error from dtsub(): %d\n", x);
else
{
/* Convert to ASCII for displaying */
intoasc(&i_ytm, out_str);
printf("%s\n", out_str);
}
/* Determine month-to-month difference */
printf("Difference (month to month) = ");
if(x = dtsub(&dt_var1, &dt_var2, &i_mtm))
printf("Error from dtsub(): %d\n", x);
else
{
/* Convert to ASCII for displaying */
intoasc(&i_mtm, out_str);
printf("%s\n", out_str);
}
/* Determine day-to-hour difference: Error - Can't convert
* year-to-month to day-to-hour
*/
printf("Difference (day to hour) = ");
if(x = dtsub(&dt_var1, &dt_var2, &i_dth))
printf("Error from dtsub(): %d\n", x);
else
{
/* Convert to ASCII for displaying */
intoasc(&i_dth, out_str);
printf("%s\n", out_str);
}
printf("\nDTSUB Sample Program over.\n\n");
}
Output
DTSUB Sample ESQL Program running.
Datetime (year to month) value #1 = 2007-10
Datetime (year to month) value #2 = 2001-08
-------------------------------------------
Difference (year to month) = 0006-02
Difference (month to month) = 86
Difference (day to hour) = Error from dtsub(): -1266
DTSUB Sample Program over.