SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
Host Variables in C/C++ Programs
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
3-27
Date-Time and Interval Data
For example, if a table in the database has this column definition:
AGE INTERVAL YEAR(2) TO MONTH
The host variable representation for 36 years, 7 months, is:
An INTERVAL host variable is represented as a seven-character string, including five
characters—with a hyphen (-) as the field separator—plus a character for the sign and
a character (empty space) for a null terminator.
Selecting Interval Values
To retrieve interval values from the database, declare an INTERVAL host variable the
same length as the number of bytes you expect to store in the array. The SQL/MX
preprocessor adds two extra characters to the interval string—one for the sign and one
(an empty space) for a null terminator.
If your C program performs string operations on the interval host variable, you must
append a null terminator to the output string before processing it because the interval
data type is internally processed as a C character string.
Example
A database contains a BILLINGS table consisting of the CUSTNUM, START_DATE,
BILLING_DATE, and TIME_BEFORE_PMT columns. This example selects a customer
number and interval value:
EXEC SQL BEGIN DECLARE SECTION;
struct billing_rec {
unsigned short hv_custnum;
DATE hv_start_date;
DATE hv_billing_date;
INTERVAL DAY(3) hv_time_before_pmt;
} bill;
...
EXEC SQL END DECLARE SECTION;
...
EXEC SQL SELECT custnum, time_before_pmt
INTO :bill.hv_custnum, :bill.hv_time_before_pmt
FROM billings
WHERE custnum = :hv_this_customer;
... bill.hv_time_before_pmt[4]='\0';
Inserting or Updating Interval Values
To insert or update interval values, format a C interval string in the desired display
format for an interval. The first character is reserved for the sign of the interval.
+ 3 6 0 7
−
Sign Year Separator Month
Null
C