Equivalent SQL and C data types
When you declare host variables in your C programs, the precompiler uses equivalent SQL data types. When you retrieve data of a particular SQL data type into a host variable, you need to ensure that the host variable is of an equivalent data type.
The following table describes the SQL data type and the base SQLTYPE and SQLLEN values that the precompiler uses for host variables in SQL statements.
C host variable data type | SQLTYPE of host variable1 | SQLLEN of host variable | SQL data type |
---|---|---|---|
|
500 | 2 | SMALLINT |
|
496 | 4 | INTEGER |
|
492 | 8 | BIGINT5 |
|
484 | p in byte 1, s in byte 2 | DECIMAL(p,s)2 |
|
996/997 | 4 | DECFLOAT(16)7, 8 |
|
996/997 | 8 | DECFLOAT(16)8 |
|
996/997 | 16 | DECFLOAT(34)8 |
|
480 | 4 | FLOAT (single precision) |
|
480 | 8 | FLOAT (double precision) |
|
912 | n | BINARY(n) |
|
908 | n | VARBINARY(n) |
|
452 | 1 | CHAR(1) |
|
460 | n | VARCHAR (n-1) |
|
448 | n | VARCHAR(n) |
|
456 | n | VARCHAR(n) |
|
468 | 1 | GRAPHIC(1) |
|
400 | n | VARGRAPHIC (n-1) |
|
464 | n | VARGRAPHIC(n) |
|
472 | n | VARGRAPHIC(n) |
|
972 | 4 | Result set locator3 |
|
976 | 4 | Table locator3 |
|
960 | 4 | BLOB locator3 |
|
964 | 4 | CLOB locator3 |
|
968 | 4 | DBCLOB locator3 |
|
404 | n | BLOB(n) |
|
408 | n | CLOB(n) |
|
412 | n | DBCLOB(n)4 |
SQL TYPE IS XML AS BLOB(n) | 404 | 0 | XML |
SQL TYPE IS XML AS CLOB(n) | 408 | 0 | XML |
SQL TYPE IS XML AS DBCLOB(n) | 412 | 0 | XML |
SQL TYPE IS BLOB_FILE | 916/917 | 267 | BLOB file reference 3 |
SQL TYPE IS CLOB_FILE | 920/921 | 267 | CLOB file reference 3 |
SQL TYPE IS DBCLOB_FILE | 924/925 | 267 | DBCLOB file reference 3 |
SQL TYPE IS XML AS BLOB_FILE | 916/917 | 267 | XML BLOB file reference 3 |
SQL TYPE IS XML AS CLOB_FILE | 920/921 | 267 | XML CLOB file reference 3 |
SQL TYPE IS XML AS DBCLOB_FILE | 924/925 | 267 | XML DBCLOB file reference 3 |
|
904 | 40 | ROWID |
Notes:
|
The following table shows equivalent C host variables for each SQL data type. Use this table to determine the C data type for host variables that you define to receive output from the database. For example, if you retrieve TIMESTAMP data, you can define a variable of NUL-terminated character form or VARCHAR structured form
This table shows direct conversions between SQL data types and C data types. However, a number of SQL data types are compatible. When you do assignments or comparisons of data that have compatible data types, DB2 converts those compatible data types.
SQL data type | C host variable equivalent | Notes |
---|---|---|
SMALLINT | short int | |
INTEGER | long int | |
DECIMAL(p,s) or NUMERIC(p,s) | decimal | You can use the double data type if your C compiler does not have a decimal data type; however, double is not an exact equivalent. |
REAL or FLOAT(n) | float | 1<=n<=21 |
DOUBLE PRECISION or FLOAT(n) | double | 22<=n<=53 |
DECFLOAT(16) | _Decminal32 | |
DECFLOAT(34) | _Decimal128 | |
BIGINT | long long, long long int, and sqlint64 | |
BINARY(n) | SQL TYPE IS BINARY(n) | 1<=n<=255 If data can contain character NULs (\0), certain C and C++ library functions might not handle the data correctly. Ensure that your application handles the data properly. |
VARBINARY(n) | SQL TYPE IS VARBINARY(n) | 1<=n<=32 704 |
CHAR(1) | single-character form | |
CHAR(n) | no exact equivalent | If n>1, use NUL-terminated character form |
VARCHAR(n) | NUL-terminated character form | If data can contain character NULs (\0), use VARCHAR structured form. Allow at least n+1 to accommodate the NUL-terminator. |
VARCHAR structured form | ||
GRAPHIC(1) | single-graphic form | |
GRAPHIC(n) | no exact equivalent | If n>1, use NUL-terminated graphic form. n is the number of double-byte characters. |
VARGRAPHIC(n) | NUL-terminated graphic form | If data can contain graphic NUL values (\0\0), use VARGRAPHIC structured form. Allow at least n+1 to accommodate the NUL-terminator. n is the number of double-byte characters. |
VARGRAPHIC structured form | n is the number of double-byte characters. | |
DATE | NUL-terminated character form | If you are using a date exit routine, that routine determines the length. Otherwise, allow at least 11 characters to accommodate the NUL-terminator. |
VARCHAR structured form | If you are using a date exit routine, that routine determines the length. Otherwise, allow at least 10 characters. | |
TIME | NUL-terminated character form | If you are using a time exit routine, the length is determined by that routine. Otherwise, the length must be at least 7; to include seconds, the length must be at least 9 to accommodate the NUL-terminator. |
VARCHAR structured form | If you are using a time exit routine, the length is determined by that routine. Otherwise, the length must be at least 6; to include seconds, the length must be at least 8. | |
TIMESTAMP | NUL-terminated character form | The length must be at least 20. To include microseconds, the length must be 27. If the length is less than 27, truncation occurs on the microseconds part. |
VARCHAR structured form | The length must be at least 19. To include microseconds, the length must be 26. If the length is less than 26, truncation occurs on the microseconds part. | |
TIMESTAMP(0) | NUL-terminated character form | The length must be at least 20. |
VARCHAR structured form | The length must be at least 19. | |
TIMESTAMP(p) p > 0 | NUL-terminated character form | The length must be at least 20. To include fractional seconds, the length must be 21+x where x is the number of fractional seconds to include; if x is less than p, truncation occurs on the fraction seconds part. |
VARCHAR structured form | The length must be at least 19. To include fractional seconds, the length must be 20+x where x is the number of fractional seconds to include; if x is less than p, truncation occurs on the fractional seconds part. | |
TIMESTAMP(0) WITH TIME ZONE | NUL-terminated character form | The length must be at least 26. |
VARCHAR structured form | The length must be at least 25. | |
TIMESTAMP(p) WITH TIME ZONE | NUL-terminated character form | The length must be at least 27+p. |
VARCHAR structured form | The length must be at least 26+p. | |
Result set locator | SQL TYPE IS RESULT_SET_LOCATOR | Use this data type only for receiving result sets. Do not use this data type as a column type. |
Table locator | SQL TYPE IS TABLE LIKE table-name AS LOCATOR | Use this data type only in a user-defined function or stored procedure to receive rows of a transition table. Do not use this data type as a column type. |
BLOB locator | SQL TYPE IS BLOB_LOCATOR | Use this data type only to manipulate data in BLOB columns. Do not use this data type as a column type. |
CLOB locator | SQL TYPE IS CLOB_LOCATOR | Use this data type only to manipulate data in CLOB columns. Do not use this data type as a column type. |
DBCLOB locator | SQL TYPE IS DBCLOB_LOCATOR | Use this data type only to manipulate data in DBCLOB columns. Do not use this data type as a column type. |
BLOB(n) | SQL TYPE IS BLOB(n) | 1≤n≤2147483647 |
CLOB(n) | SQL TYPE IS CLOB(n) | 1≤n≤2147483647 |
DBCLOB(n) | SQL TYPE IS DBCLOB(n) | n is the number of double-byte characters. 1≤n≤1073741823 |
XML | SQL TYPE IS XML AS BLOB(n) | 1≤n≤2147483647 |
XML | SQL TYPE IS XML AS CLOB(n) | 1≤n≤2147483647 |
XML | SQL TYPE IS XML AS DBCLOB(n) | n is the number of double-byte characters. 1≤n≤1073741823 |
BLOB file reference | SQL TYPE IS BLOB_FILE | Use this data type only to manipulate data in BLOB columns. Do not use this data type as a column type. |
CLOB file reference | SQL TYPE IS CLOB_FILE | Use this data type only to manipulate data in CLOB columns. Do not use this data type as a column type. |
DBCLOB file reference | SQL TYPE IS DBCLOB_FILE | Use this data type only to manipulate data in DBCLOB columns. Do not use this data type as a column type. |
XML BLOB file reference | SQL TYPE IS XML AS BLOB_FILE | Use this data type only to manipulate XML data as BLOB files. Do not use this data type as a column type. |
XML CLOB file reference | SQL TYPE IS XML AS CLOB_FILE | Use this data type only to manipulate XML data as CLOB files. Do not use this data type as a column type. |
XML DBCLOB file reference | SQL TYPE IS XML AS DBCLOB_FILE | Use this data type only to manipulate XML data as DBCLOB files. Do not use this data type as a column type. |
ROWID | SQL TYPE IS ROWID |