IBM Books

Call Level Interface Guide and Reference

Data Types and Data Conversion

When writing a DB2 CLI application it is necessary to work with both SQL data types and C data types. This is unavoidable since the DBMS uses SQL data types, and the application must use C data types. This means the application must match C data types to SQL data types when transferring data between the DBMS and the application (when calling DB2 CLI functions).

To help address this, DB2 CLI provides symbolic names for the various data types, and manages the transfer of data between the DBMS and the application. It will also perform data conversion (from a C character string to an SQL INTEGER type, for example) if required. To accomplish this, DB2 CLI needs to know both the source and target data type. This requires the application to identify both data types using symbolic names.

C and SQL Data Types

Table 3 list each of the SQL data types, with its corresponding symbolic name, and the default C symbolic name.

SQL Data Type
This column contains the SQL data types as they would appear in an SQL CREATE DDL statement. The SQL data types are dependent on the DBMS.

Symbolic SQL Data Type
This column contains a SQL symbolic names that are defined (in sqlcli.h) as an integer value. These values are used by various functions to identify the SQL data types listed in the first column. Refer to Example for an example using these values.

Default C Symbolic Data Type
This column contains C symbolic names, also defined as an integer values. These values are used in various functions arguments to identify the C data type as shown in Table 4. The symbolic names are used by various functions, (such as SQLBindParameter(), SQLGetData(), SQLBindCol(), etc.) to indicate the C data types of the application variables. Instead of explicitly identifying the C data type when calling these functions, SQL_C_DEFAULT can be specified instead, and DB2 CLI will assume a default C data type based on the SQL data type of the parameter or column as shown by this table. For example, the default C data type of SQL_DECIMAL is SQL_C_CHAR.


Table 3. SQL Symbolic and Default Data Types
SQL Data Type Symbolic SQL Data Type Default Symbolic C Data Type
BLOB SQL_BLOB SQL_C_BINARY
BLOB LOCATOR a SQL_BLOB_LOCATOR SQL_C_BLOB_LOCATOR
CHAR SQL_CHAR SQL_C_CHAR
CHAR FOR BIT DATA b SQL_BINARY SQL_C_BINARY
CLOB SQL_CLOB SQL_C_CHAR
CLOB LOCATOR a SQL_CLOB_LOCATOR SQL_C_CLOB_LOCATOR
DATE SQL_TYPE_DATE d SQL_C__TYPE_DATE d
DBCLOB SQL_DBCLOB SQL_C_DBCHAR
DBCLOB LOCATOR a SQL_DBCLOB_LOCATOR SQL_C_DBCLOB_LOCATOR
DECIMAL SQL_DECIMAL SQL_C_CHAR
DOUBLE SQL_DOUBLE SQL_C_DOUBLE
FLOAT SQL_FLOAT SQL_C_DOUBLE
GRAPHIC SQL_GRAPHIC SQL_C_DBCHAR
INTEGER SQL_INTEGER SQL_C_LONG
LONG VARCHAR b SQL_LONGVARCHAR SQL_C_CHAR
LONG VARCHAR FOR BIT DATA b SQL_LONGVARBINARY SQL_C_BINARY
LONG VARGRAPHIC b SQL_LONGVARGRAPHIC SQL_C_DBCHAR
NUMERIC c SQL_NUMERIC c SQL_C_CHAR
REAL SQL_REAL SQL_C_FLOAT
SMALLINT SQL_SMALLINT SQL_C_SHORT
TIME SQL_TYPE_TIME d SQL_C_TYPE_TIME d
TIMESTAMP SQL_TYPE_TIMESTAMP d SQL_C_TYPE_TIMESTAMP d
VARCHAR SQL_VARCHAR SQL_C_CHAR
VARCHAR FOR BIT DATA b SQL_VARBINARY SQL_C_BINARY
VARGRAPHIC SQL_VARGRAPHIC SQL_C_DBCHAR

a LOB locator types are not persistent SQL data types, (columns can not be defined with a locator type, they are only used to describe parameter markers, or to represent a LOB value), refer to "Using Large Objects"
b LONG data types and FOR BIT DATA data types should be replaced by an appropriate LOB types whenever possible.
c NUMERIC is a synonym for DECIMAL on DB2 for MVS/ESA, DB2 for VSE & VM and DB2 Universal Database.
d See Appendix B. "Migrating Applications" for information on what data type was used in previous releases.

Note:The data types, DATE, DECIMAL, NUMERIC, TIME, and TIMESTAMP cannot be transferred to their default C buffer types without a conversion.

Table 4 shows the generic type definitions for each symbolic C type.

C Symbolic Data Type
This column contains C symbolic names, defined as integer values. These values are used in various functions arguments to identify the C data type shown in the last column. Refer to Example for an example using these values.

C Type
This column contains C defined types, defined in sqlcli.h using a C typedef statement. The values in this column should be used to declare all DB2 CLI related variables and arguments, in order to make the application more portable. Refer to Table 6 for a list of additional symbolic data types used for function arguments.

Base C type
This column is shown for reference only, all variables and arguments should be defined using the symbolic types in the previous column. Some of the values are C structures that are described in Table 5.


Table 4. C Data Types
C Symbolic Data Type C Type Base C type
SQL_C_CHAR SQLCHAR unsigned char
SQL_C_BIT SQLCHAR unsigned char or char (Value 1 or 0)
SQL_C_TINYINT SQLSCHAR signed char (Range -128 to 127)
SQL_C_SHORT SQLSMALLINT short int
SQL_C_LONG SQLINTEGER long int
SQL_C_DOUBLE SQLDOUBLE double
SQL_C_FLOAT SQLREAL float
SQL_C_TYPE_DATE b DATE_STRUCT see Table 5
SQL_C_TYPE_TIME b TIME_STRUCT see Table 5
SQL_C_TYPE_TIMESTAMP b TIMESTAMP_STRUCT see Table 5
SQL_C_CLOB_LOCATOR a SQLINTEGER long int
SQL_C_BINARY SQLCHAR unsigned char
SQL_C_BLOB_LOCATOR a SQLINTEGER long int
SQL_C_DBCHAR SQLDBCHAR wchar_t
SQL_C_DBCLOB_LOCATOR SQLINTEGER long int
a LOB Locator Types.
b See Appendix B. "Migrating Applications" for information on what data type was used in previous releases.

Note:fcSQL file reference data types (used in embedded SQL) are not needed in DB2 CLI, refer to "Using Large Objects"


Table 5. C DATE, TIME, and TIMESTAMP Structures
C Type Generic Structure Windows Structure
DATE_STRUCT
 
typedef struct DATE_STRUCT
  {
    SQLSMALLINT   year;
    SQLUSMALLINT   month;
    SQLUSMALLINT   day;
  } DATE_STRUCT;

 
typedef struct tagDATE_STRUCT
  {
    SWORD   year;
    UWORD   month;
    UWORD   day;
  } DATE_STRUCT;

TIME_STRUCT
 
typedef struct TIME_STRUCT
  {
    SQLUSMALLINT   hour;
    SQLUSMALLINT   minute;
    SQLUSMALLINT   second;
  } TIME_STRUCT;

 
typedef struct tagTIME_STRUCT
  {
    UWORD   hour;
    UWORD   minutes;
    UWORD   second;
  } TIME_STRUCT;

TIMESTAMP_STRUCT
 
typedef struct TIMESTAMP_STRUCT
  {
    SQLUSMALLINT   year;
    SQLUSMALLINT   month;
    SQLUSMALLINT   day;
    SQLUSMALLINT   hour;
    SQLUSMALLINT   minute;
    SQLUSMALLINT   second;
    SQLINTEGER    fraction;
  } TIMESTAMP_STRUCT;

 
typedef struct tagTIMESTAMP_STRUCT
  {
    SWORD   year;
    UWORD   month;
    UWORD   day;
    UWORD   hour;
    UWORD   minute;
    UWORD   second;
    UDWORD  fraction;
  } TIMESTAMP_STRUCT;

Refer to Table 6 for more information on the SQLUSMALLINT C data type.

Other C Data Types

As well as the data types that map to SQL data types, there are also C symbolic types used for other function arguments, such as pointers and handles. Both the generic and ODBC data types are shown below.

Table 6. C Data Types and Base C Data Types
Defined C Type Base C Type Typical Usage
SQLPOINTER void * Pointers to storage for data and parameters.
SQLHANDLE long int Handle used to reference all 4 types of handle information.
SQLHENV long int Handle referencing environment information.
SQLHDBC long int Handle referencing database connection information.
SQLHSTMT long int Handle referencing statement information.
SQLUSMALLINT unsigned short int Function input argument for unsigned short integer values.
SQLUINTEGER unsigned long int Function input argument for unsigned long integer values.
SQLRETURN short int Return code from DB2 CLI functions.

Versions of DB2 CLI prior to Version 2.1:

  • Defined SQLRETURN as a long (32-bit) integer.
  • Used SQLSMALLINT and SQLINTEGER instead of SQLUSMALLINT and SQLUINTEGER (signed instead of unsigned). Refer to Appendix B. "Migrating Applications" for more information.

Data Conversion

As mentioned previously, DB2 CLI manages the transfer and any required conversion of data between the application and the DBMS. Before the data transfer actually takes place, the source, target or both data types are indicated when calling SQLBindParameter(), SQLBindCol() or SQLGetData(). These functions use the symbolic type names shown in Table 3, to identify the data types involved.

For example, to bind a parameter marker that corresponds to an SQL data type of DECIMAL(5,3), to an application's C buffer type of double, the appropriate SQLBindParameter() call would look like:

     SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_DOUBLE,
                       SQL_DECIMAL, 5, 3, double_ptr, 0, NULL);

Table 3 shows only the default data conversions. The functions mentioned in the previous paragraph can be used to convert data to other types, but not all data conversions are supported or make sense. Table 7 shows all the conversions supported by DB2 CLI.

The first column in Table 7 contains the data type of the SQL data type, the remaining columns represent the C data types. If the C data type columns contains:

D
The conversion is supported and is the default conversion for the SQL data type.
X
all IBM DBMSs support the conversion,
blank
no IBM DBMS supports the conversion.

As an example, the table indicates that a CHAR (or a C character string as indicated in Table 7) can be converted into a SQL_C_LONG (a signed long). In contrast, a LONGVARCHAR cannot be converted to a SQL_C_LONG.

Refer to Appendix F. "Data Conversion" for information about the required formats and the results of converting between data types.

Limits on precision, and scale, as well as truncation and rounding rules for type conversions follow rules specified in the SQL Reference with the following exception; truncation of values to the right of the decimal point for numeric values may return a truncation warning, whereas truncation to the left of the decimal point returns an error. In cases of error, the application should call SQLGetDiagRec() to obtain the SQLSTATE and additional information on the failure. When moving and converting floating point data values between the application and DB2 CLI, no correspondence is guaranteed to be exact as the values may change in precision and scale.

Table 7. Supported Data Conversions
SQL Data Type

S 
Q
L
_
C
_
C
H
A
R

S 
Q
L
_
C
_
L
O
N
G

S 
Q
L
_
C
_
S
H
O
R
T

S 
Q
L
_
C
_
T
I
N
Y
I
N
T

 
S
Q
L
_
C
_
F
L
O
A
T

S 
Q
L
_
C
_
D
O
U
B
L
E

S 
Q
L
_
C
_
T
Y
P
E
_
D
A
T
E

S 
Q
L
_
C
_
T
Y
P
E
_
T
I
M
E

S 
Q
L
_
C
_
T
Y
P
E
_
T
I
M
E
S
T
A
M
P

S 
Q
L
_
C
_
B
I
N
A
R
Y

S 
Q
L
_
C
_
B
I
T

S 
Q
L
_
C
_
D
B
C
H
A
R

S 
Q
L
_
C
_
C
L
O
B
_
L
O
C
A
T
O
R

S 
Q
L
_
C
_
B
L
O
B
_
L
O
C
A
T
O
R

S 
Q
L
_
C
_
D
B
C
L
O
B
_
L
O
C
A
T
O
R

BLOB

X

 

 

 

 

 

 

 

 

D

 

 

 

X



CHAR    

D

X

X

X

X

X

X

X

X

X

X

 

 

 



CLOB 

D

 

 

 

 

 

 

 

 

X

 

 

X

 



DATE    

X

 

 

 

 

 

D

 

X

 

 

 

 

 



DBCLOB  

 

 

 

 

 

 

 

 

 

X

 

D

 

 

X

DECIMAL  

D

X

X

X

X

X

 

 

 

 

X

 

 

 



DOUBLE  

X

X

X

X

X

D

 

 

 

 

X

 

 

 



FLOAT   

X

X

X

X

X

D

 

 

 

 

X

 

 

 



GRAPHIC 

X

 

 

 

 

 

 

 

 

 

 

D

 

 



INTEGER 

X

D

X

X

X

X

 

 

 

 

X

 

 

 



LONG VARCHAR    

D

 

 

 

 

 

 

 

 

X

 

 

 

 



LONG VARGRAPHIC 

X

 

 

 

 

 

 

 

 

X

 

D

 

 



NUMERIC 

D

X

X

X

X

X

 

 

 

 

X

 

 

 



REAL    

X

X

X

X

D

X

 

 

 

 

X

 

 

 



SMALLINT        

X

X

D

X

X

X

 

 

 

 

X

 

 

 



TIME            

X

 

 

 

 

 

 

D

X

 

 

 

 

 



TIMESTAMP       

X

 

 

 

 

 

X

X

D

 

 

 

 

 



VARCHAR         

D

X

X

X

X

X

X

X

X

X

X

 

 

 



VARGRAPHIC      

X

 

 

 

 

 

 

 

 

 

 

D

 

 



Note:Data is not converted to LOB Locator types, rather locators represent a data value, refer to "Using Large Objects" for more information.

REAL is not supported by DB2 Universal Database.

NUMERIC is a synonym for DECIMAL on DB2 for MVS/ESA, DB2 for VSE & VM, and DB2 Universal Database.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]