SQL to C: Character

The identifiers for the character ODBC SQL data types are the following:

  • SQL_CHAR
  • SQL_VARCHAR
  • SQL_LONGVARCHAR
  • SQL_WCHAR
  • SQL_WVARCHAR
  • SQL_WLONGVARCHAR

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

C type identifier Test TargetValuePtr StrLen_or_IndPtr SQLSTATE
SQL_C_CHAR Byte length of data < BufferLength

Byte length of data >= BufferLength
Data

Truncated data
Length of data in bytes

Length of data in bytes
n/a

01004
SQL_C_WCHAR Character length of data < BufferLength

Character length of data >= BufferLength
Data

Truncated data
Length of data in characters

Length of data in characters
n/a

01004
SQL_C_STINYINT SQL_C_UTINYINT SQL_C_TINYINT SQL_C_SBIGINT SQL_C_UBIGINT SQL_C_SSHORT SQL_C_USHORT SQL_C_SHORT SQL_C_SLONG SQL_C_ULONG SQL_C_LONG SQL_C_NUMERIC Data converted without truncation[b]

Data converted with truncation of fractional digits[a]

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

Data is not a numeric-literal[b]
Data

Truncated data

Undefined

Undefined
Number of bytes of the C data type

Number of bytes of the C data type

Undefined

Undefined
n/a

01S07

22003

22018
SQL_C_FLOAT SQL_C_DOUBLE Data is within the range of the data type to which the number is being converted[a]

Data is outside the range of the data type to which the number is being converted[a]

Data is not a numeric-literal[b]
Data

Undefined

Undefined
Size of the C data type

Undefined

Undefined
n/a

22003

22018
SQL_C_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
Data

Truncated data

Undefined

Undefined
1[b]

1[b]

Undefined

Undefined
n/a

01S07

22003

22018
SQL_C_BINARY Byte length of data <= BufferLength

Byte length of data > BufferLength
Data

Truncated data
Length of data in bytes

Length of data
n/a

01004
SQL_C_TYPE_DATE Data value is a valid date-value[a]

Data value is a valid timestamp-value; time portion is zero[a]

Data value is a valid timestamp-value; time portion is nonzero[a],[c]

Data value is not a valid date-value or timestamp-value[a]
Data

Data

Truncated data

Undefined
6[b]

6[b]

6[b]

Undefined
n/a

n/a

01S07

22018
SQL_C_TYPE_TIME Data value is a valid time-value and the fractional seconds value is 0[a]

Data value is a valid timestamp-value or a valid time-value; fractional seconds portion is zero[a],[d]

Data value is a valid timestamp-value; fractional seconds portion is nonzero[a],[d],[e]

Data value is not a valid time-value or timestamp-value[a]
Data

Data

Truncated data

Undefined
6[b]

6[b]

6[b]

Undefined
n/a

n/a

01S07

22018
SQL_C_TYPE_TIMESTAMP Data value is a valid timestamp-value or a valid time-value; fractional seconds portion not truncated[a]

Data value is a valid timestamp-value or a valid time-value; fractional seconds portion truncated[a]

Data value is a valid date-value[a]

Data value is a valid time-value[a]

Data value is not a valid date-value, time-value, or timestamp-value[a]
Data

Truncated data

Data[f]

Data[g]

Undefined
16[b]

16[b]

16[b]

16[b]

Undefined
n/a

01S07

n/a

n/a

22018
All C interval types Data value is a valid interval value; no truncation

Data value is a valid interval value; truncation of one or more trailing fields

Data is valid interval; leading field significant precision is lost

The data value is not a valid interval value
Data

Truncated data

Undefined

Undefined
Length of data in bytes

Length of data in bytes

Undefined

Undefined
n/a

01S07

22015

22018

[a] The value of BufferLength is ignored for this conversion. The driver assumes that the size of *TargetValuePtr is the size of the C data type.

[b] This is the size of the corresponding C data type.

[c] The time portion of the timestamp-value is truncated.

[d] The date portion of the timestamp-value is ignored.

[e] The fractional seconds portion of the timestamp is truncated.

[f] The time fields of the timestamp structure are set to zero.

[g] The date fields of the timestamp structure are set to the current date.

Extra spaces

Leading and trailing spaces are ignored when SQL character data is converted to any of the following types:

  • date
  • numeric
  • time
  • timestamp
  • interval C data