C to SQL: Character

The identifiers for the character ODBC C data type are:

SQL_C_CHAR
SQL_C_WCHAR

The following table shows the ODBC SQL data types to which C character data may be converted. For an explanation of the columns and terms in the table, see “Converting Data from C to SQL Data Types.”

Note   When character C data is converted to Unicode SQL data, then the length of the Unicode data must be an even number.

SQL type identifier Test SQLSTATE
SQL_CHAR
SQL_VARCHAR
SQL_LONGVARCHAR
Byte length of data <= Column length.

Byte length of data > Column length.

n/a

22001

SQL_WCHAR
SQL_WVARCHAR
SQL_WLONGVARCHAR
Character length of data <= Column length.

Character length of data > Column length.

n/a

22001

SQL_DECIMAL
SQL_NUMERIC
SQL_TINYINT
SQL_SMALLINT
SQL_INTEGER
SQL_BIGINT
Data converted without truncation.

Data converted with truncation of fractional digits. [e]

Conversion of data would result in loss of whole (as opposed to fractional) digits. [e]

Data value is not a numeric-literal.

n/a

22001

22001

22018

SQL_REAL
SQL_FLOAT
SQL_DOUBLE
Data is within the range of the data type to which the number is being converted.

Data is outside the range of the data type to which the number is being converted.

Data value is not a numeric-literal.

n/a

22003

22018

SQL_BIT Data is 0 or 1.

Data is greater than 0, less than 2, and not equal to 1.

Data is less than 0 or greater than or equal to 2.

Data is not a numeric-literal.

n/a

22001

22003

22018

SQL_BINARY
SQL_VARBINARY
SQL_LONGVARBINARY
(Byte length of data) / 2 <= column byte length.

(Byte length of data) / 2 > column byte length.

Data value is not a hexadecimal value.

n/a

22001

22018

SQL_TYPE_DATE Data value is a valid ODBC-date-literal.

Data value is a valid ODBC-timestamp-literal; time portion is zero.

Data value is a valid ODBC-timestamp-literal; time portion is non-zero. [a]

Data value is not a valid ODBC-date-literal or ODBC-timestamp-literal.

n/a

n/a

22008

22018

SQL_TYPE_TIME Data value is a valid ODBC-time-
literal.

Data value is a valid ODBC-timestamp-literal; fractional seconds portion is zero. [b]

Data value is a valid ODBC-timestamp-literal; fractional seconds portion is non-zero. [b]

Data value is not a valid ODBC-time-
literal
or ODBC-timestamp-literal.

n/a

n/a

22008

22018

SQL_TYPE_TIMESTAMP Data value is a valid ODBC-
timestamp-literal
; fractional seconds portion not truncated.

Data value is a valid ODBC-timestamp-literal; fractional seconds portion truncated.

Data value is a valid ODBC-date-
literal.
[c]

Data value is a valid ODBC-time-
literal.
[d]

Data value is not a valid ODBC-date-
literal
, ODBC-time-literal, or ODBC-
timestamp-literal.

n/a

22008

n/a

n/a

22018

All SQL interval types Data value is a valid interval value; no truncation occurs.

Data value is a valid interval value; the value in one of the fields is truncated.

The data value is not a valid interval literal.

n/a

22015

22018


[a]The time portion of the timestamp is truncated.

[b]The date portion of the timestamp is ignored.

[c]The time portion of the timestamp is set to zero.

[d]The date portion of the timestamp is set to the current date.

[e]The driver/data source effectively waits until the entire string has been received (even if the character data is sent in pieces by calls to SQLPutData) before attempting to perform the conversion.

When character C data is converted to numeric, date, time, or timestamp SQL data, leading and trailing blanks are ignored.

When character C data is converted to binary SQL data, each two bytes of character data are converted to a single byte (8 bits) of binary data. Each two bytes of character data represent a number in hexadecimal form. For example, “01” is converted to a binary 00000001 and “FF” is converted to a binary 11111111.

The driver always converts pairs of hexadecimal digits to individual bytes and ignores the null-termination byte. Because of this, if the length of the character string is odd, the last byte of the string (excluding the null-termination byte, if any) is not converted.

Note   Application developers are discouraged from binding character C data to a binary SQL data type. This conversion is usually inefficient and slow.