SQL/MX 3.2 Programming Manual for C and COBOL (H06.25+, J06.14+)

Host Variables in C/C++ Programs
HP NonStop SQL/MX Release 3.2 Programming Manual for C and COBOL663854-002
3-34
Date-Time and Interval Data
Example
Suppose that an SQL/MP database has a BILLINGS table that consists of the
CUSTNUM and BILLING_DATE columns. The BILLING_DATE column has a
DATETIME MONTH TO DAY data type, which has no equivalent in SQL/MX. This
example inserts a customer number and date-time value into that table:
EXEC SQL BEGIN DECLARE SECTION;
struct billing_rec {
unsigned short hv_custnum;
char hv_billing_date[6];
...
} bill;
...
EXEC SQL END DECLARE SECTION;
...
bill.hv_billing_date[5]='\0';
...
EXEC SQL INSERT INTO billings
VALUES (:bill.hv_custnum,
CAST(:bill.hv_billing_date AS DATETIME MONTH TO DAY));
...
INTERVAL Representation
Interval values are represented as character strings, with a separator between the
values of the fields (year-month or day-time). An extra character is generated at the
beginning of the interval string for a sign.
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.
C
+ 3 6 0 7
Sign Year Separator Month
Null