Previous PageTop Of PageTable Of ContentsNext Page


Appendix D . Data Types

D.1 ODBC SQL Data Types
D.2 ODBC C Data Types
D.3 Default ODBC C Data Types
D.4 Precision, Scale, Length, and Display Size
D.5 Data Type Conversions


D. Data Types

A driver maps data source-specific SQL data types to ODBC SQL data types and driver-specific SQL data types. Each SQL data type corresponds to an ODBC C data type. The application can specify the correct C data type with the fCType argument in SQLBindCol, SQLGetData, or SQLBindParameter. Before sending data to the data source, the driver converts it from the specified C data type. Before retrieving data from the data source, the driver converts it to the specified C data type.

In the following sections, we will discuss :

ODBC SQL data types
ODBC C data types
Default ODBC C data types
Precision, scale, length and display size of SQL data types
Data type conversions

D.1 ODBC SQL Data Types

The table on the following page lists the mapping between ODBC SQL data types (the fSqlType column) and the corresponding DBMaker SQL data types (SQL Data Type column). A description of the data types is also listed in the table.

fSqlType

SQL Data Type

Description

SQL_CHAR

CHAR(n)

Character string of fixed length n (1 <= n <= 3992)

SQL_VARCHAR

VARCHAR(n)

Variable-length character string with a maximum length n (1 <= n <= 3992)

SQL_LONGVARCHAR

LONG VARCHAR

Variable length character data with a maximum length of 2 GB

SQL_DECIMAL

DECIMAL(p,s) (1)

DECIMAL(p)

DECIMAL

Signed exact, numeric value with precision p and scale s (1<= p <= 17; 0 <= s <= p)

(6<= p <= 17; s = 6)

(p = 17; s = 6)

SQL_SMALLINT

SMALLINT

Exact signed numeric value with precision 5 and scale 0 (32,768 <= n <= 32,767)

SQL_INTEGER

INTEGER

Exact signed numeric value with precision 10 and scale 0 (-231 <= n <= 231 -1)

SQL_REAL (2)

FLOAT

Signed, approximate, numeric value with a mantissa of precision 7 (10-38 to 1038)

SQL_FLOAT

DOUBLE

Signed, approximate, numeric value with a mantissa of precision 15 (10^-308 to 10^308)

SQL_DOUBLE

DOUBLE

Signed, approximate, numeric value with a mantissa of precision 15 (10^-308 to 10^308)

SQL_FILE (3)

FILE

DBMaker specific data type. Stores the data of a FILE column as an external file.

SQL_BINARY

BINARY(n)

Binary data of fixed length n (1 <= n <= 3992)

SQL_LONGVARBINARY

LONG VARBINARY

Variable length binary data with a maximum length of 2 GB

SQL_DATE

DATE

Date data

SQL_TIME

TIME

Time data

SQL_TIMESTAMP

TIMESTAMP

Includes both date and time data

D.2 ODBC C Data Types

The ODBC C data type is the data type used by an application to store data. The C data type used is specified in the SQLBindCol, SQLGetData and SQLBindParameter functions with the fCType argument. The table on the following page lists the valid fCType values, the ODBC C data type that implements each fCType value and the corresponding C type both in PC and UNIX environments.

fcType

ODBC C Typedef

C Type

Bytes

SQL_C_CHAR

UCHAR FAR *

unsigned char FAR *

4

SQL_C_SSHORT

SWORD

short int

2

SQL_C_SHORT

SWORD

short int

2

SQL_C_USHORT

UWORD

unsigned short int

2

SQL_C_SLONG

SDWORD

long int

4

SQL_C_LONG

Same as SQL_C_SLONG

   

SQL_C_ULONG

UDWORD

unsigned long int

4

SQL_C_FLOAT

SFLOAT

float

4

SQL_C_DOUBLE

SDOUBLE

double

8

SQL_C_BINARY

UCHAR FAR *

unsigned char FAR *

4

SQL_C_DATE

DATE_STRUCT

struct tagDATE_STRUCT
{
SWORD year; (4)
UWORD
month; (5)
UWORD day; (6)
}

6

SQL_C_TIME

TIME_STRUCT

struct tagTIME_STRUCT
{
UWORD hour; (7)
UWORD minutes; (8)
UWORD second; (9)
}

6

SQL_C_TIMESTAMP

TIMESTAMP_STRUCT

struct tagTIMESTAMP_STRUCT
{
SWORD year; (4)
UWORD month; (5)
UWORD day; (6)
UWORD hour; (7)
UWORD minutes; (8)
UWORD second; (9)
UDWORD fraction; (10)
}

16

SQL_C_DEFAULT

see the following section "Default C Data Types"

D.3 Default ODBC C Data Types

If an application specifies SQL_C_DEFAULT for the fCType argument in SQLBindCol, SQLGetData, or SQLBindParameter, the driver assumes that the C data type of the output or input buffer corresponds to the SQL data type of the column or parameter to which the buffer is bound. The following table shows the default C data type for each ODBC SQL data type.

SQL Data Type

Default C Data Type

SQL_CHAR

SQL_C_CHAR

SQL_VARCHAR

SQL_C_CHAR

SQL_LONGVARCHAR

SQL_C_CHAR

SQL_DECIMAL

SQL_C_CHAR

SQL_FILE (11)

SQL_C_CHAR

SQL_SMALLINT

SQL_C_SSHORT

SQL_INTEGER

SQL_C_SLONG

SQL_REAL

SQL_C_FLOAT

SQL_FLOAT

SQL_C_FLOAT

SQL_DOUBLE

SQL_C_DOUBLE

SQL_BINARY

SQL_C_BINARY

SQL_LONGVARBINARY

SQL_C_BINARY

SQL_DATE

SQL_C_DATE

SQL_TIME

SQL_C_TIME

SQL_TIMESTAMP

SQL_C_TIMESTAMP

D.4 Precision, Scale, Length, and Display Size

SQLColAttributes, SQLColumns, and SQLDescribeCol return the precision, scale, length, and display size of a column in a table. SQLDescribeParam returns the precision or scale of a parameter in an SQL statement. SQLBindParameter sets the precision or scale of a parameter in an SQL statement. SQLGetTypeInfo returns the maximum precision and the minimum and maximum scales of an SQL data type on a data source. The following table lists the precision, scale, length and display size for each ODBC SQL type. A `-` means the value has no meaning or the corresponding type cannot be determined. For example, scale is not applicable for dates, and the precision of SQL_LONGVARCHAR can not be determined as it only refers to how many bytes of data are stored.

fSqlType

SQL Data Type

Precision (12)

Scale (13)

Length

Display Size

SQL_CHAR

CHAR(n)

n

-

n

n

SQL_VARCHAR

VARCHAR(n)

n

-

n

n

SQL_LONGVARCHAR

LONG VARCHAR

-

-

-

-

SQL_DECIMAL (14)

DECIMAL(p,s)

p

s

(p+3)/2

p+2

SQL_FILE3

FILE

79

-

79

-

SQL_SMALLINT

SMALLINT

5

0

2

6

SQL_INTEGER

INTEGER

10

0

4

11

SQL_REAL

FLOAT

7

-

4

13

SQL_FLOAT

FLOAT

7

-

4

13

SQL_DOUBLE

DOUBLE

15

-

8

22

SQL_BINARY

BINARY(n)

n

-

n

2n

SQL_LONGVARBINARY

LONG VARBINARY

-

-

-

-

SQL_DATE

DATE

11

-

6

11

SQL_TIME

TIME

15

3

6

15

SQL_TIMESTAMP

TIMESTAMP

27

3

16

27

D.5 Data Type Conversions

Two sections are used to explain data type conversions: one is for SQL to C data conversions, the other is for C to SQL data conversions. In each section, we also illustrate the examples to show the results of specified data type conversions.

SQL to C Data Conversion

Before retrieving data with SQLFetch, you should specify the data type to which the retrieved data is converted in the argument fCType of SQLBindCol. Finally, the driver will store the data in the location pointed to by the rgbValue argument in SQLBindCol. SQLGetData has the similar situation. The table on the following page lists the data type conversions, from SQL to C data type, which are provided by DBMaker.

The table on the following page shows the examples which illustrate how DBMaker converts SQL data to C data. Differences from the ODBC standard are listed in the footnotes at the bottom of the page.

SQL Data Type

SQL Data Value

C Data Type

C Len

C Data Value

sqlstate

SQL_CHAR

abcdef

SQL_C_CHAR

7

abcdef\0

N/A

SQL_CHAR

abcdef

SQL_C_CHAR

6

abcde\0

01004

SQL_CHAR

abcdef

SQL_C_BINARY

6

abcdef

N/A

SQL_CHAR

abcdef

SQL_C_BINARY

5

abcde

01004

SQL_VARCHAR

same as SQL_CHAR

SQL_LONGVARCHAR

same as SQL_CHAR

SQL_DECIMAL

1234.56

SQL_C_CHAR

8

1234.56\0

N/A

SQL_DECIMAL

1234.56

SQL_C_CHAR

5

1234\0

01004

SQL_DECIMAL

1234.56

SQL_C_CHAR

4

-

22003

SQL_DECIMAL

1234.56

SQL_C_FLOAT

-

1234.56

N/A

SQL_DECIMAL

1234.56

SQL_C_SHORT

-

1234

01004

SQL_SMALLINT

7890

SQL_C_CHAR

5

7890\0

N/A

SQL_SMALLINT

7890

SQL_C_CHAR

4

-

22003

SQL_SMALLINT

7890

SQL_C_LONG

-

7890

N/A

SQL_INTEGER

65000

SQL_C_LONG

-

65000

N/A

SQL_INTEGER

65000

SQL_C_SSHORT

-

-

22003

SQL_DOUBLE

1.2345678

SQL_C_DOUBLE

-

1.2345678

N/A

SQL_DOUBLE

1.2345678

SQL_C_FLOAT

-

1.234567

N/A

SQL_DOUBLE

1.2345678

SQL_C_SHORT

-

1

01004

SQL_FLOAT

same as SQL_DOUBLE

SQL_REAL

same as SQL_DOUBLE

SQL_DATE

1995-11-29

SQL_C_CHAR

11

1995-11-29\0

N/A

SQL_DATE

1995-11-29

SQL_C_CHAR

10

-

22003 (15)

SQL_DATE

1995-11-29

SQL_C_CHAR

10

11-29-95\0

N/A (16)

SQL_TIME

22:11:33.32

SQL_C_CHAR

19

22:11:33.320\0

N/A

SQL_TIME

22:11:33.32

SQL_C_CHAR

12

22:11:33.32\0

01004 (17)

SQL_TIME

22:11:33.32

SQL_C_CHAR

11

22:11:33.3\0

010043

SQL_TIME

22:11:33.32

SQL_C_CHAR

10

22:11:33\0

010041

SQL_TIME

22:11:33.32

SQL_C_CHAR

9

22:11:33\0

010043

SQL_TIME

22:11:33.32

SQL_C_CHAR

8

-

22003 (18)

SQL_TIME

22:11:33.32

SQL_C_CHAR

 

10 PM\0

N/A (19)

SQL_TIMESTAMP

1995-11-29 23:54:38.234

SQL_C_CHAR

24

1995-11-29 23:54:38.234\0

N/A

SQL_TIMESTAMP

1995-11-29 23:54:38.234

SQL_C_CHAR

22

1995-11-29 23:54:38.2\0

01004 (20)

SQL_TIMESTAMP

1995-11-29 23:54:38.234

SQL_C_CHAR

19

-

22003 (21)

SQL_TIMESTAMP

1995-11-29 23:54:38.234

SQL_C_CHAR

19

11/29/95 23:54:38\0

N/A (22)

SQL_BINARY

ABCDEFG

SQL_C_CHAR

15

65666768696A6B\0

N/A

SQL_BINARY

ABCDEFG

SQL_C_CHAR

14

65666768696A6\0

01004

SQL_BINARY

ABCDEFG

SQL_C_BINARY

7

ABCDEFG

N/A

SQL_BINARY

ABCDEFG

SQL_C_BINARY

6

ABCDEF

01004

SQL_LONGVARBINARY

same as SQL_BINARY

SQL_FILE

abcdefg

SQL_C_CHAR

8

abcdefg\0

N/A (23)

SQL_FILE

abcdefg

SQL_C_FILE

8

student\0

N/A (24)

C to SQL Data Conversion

When an application calls SQLExecute or SQLExecDirect, the driver retrieves the data for any parameters bound with SQLBindParameter from storage locations in the application. If necessary, before the driver sends the data to the data source, it converts the data from the data type specified by the fCType argument in SQLBindParameter to the data type specified by the fSqlType argument in SQLBindParameter. For data-at-execution parameters, the application sends the parameter with SQLPutData. The following table show the supported conversions from ODBC C data types to ODBC SQL data types by DBMaker. Any differences between DBMaker and the ODBC 3.0 specification are shown at the bottom of the page.

Note: The behavior of the SQL_C_LONG data type is the same as the SQL_C_SLONG data type.

The following table shows the examples which illustrate how DBMaker converts C data to SQL data Any differences between DBMaker and the ODBC 3.0 specification are shown at the bottom of the page.

C Data Type

C Data Value

SQL Data Type

Col Len

SQL Data

sqlstate

SQL_C_CHAR

abcdef\0

SQL_CHAR

6

abcdef

N/A

SQL_C_CHAR

abcdef\0

SQL_CHAR

5

abcde

01004

SQL_C_CHAR

1234.56\0

SQL_DECIMAL

6,2

1234.56

N/A

SQL_C_CHAR

1234.56\0

SQL_DECIMAL

5,1

1234.5

01004

SQL_C_CHAR

1234.56\0

SQL_DECIMAL

6,3

-

N/A

SQL_C_CHAR

1234\0

SQL_INTEGER

-

1234

N/A

SQL_C_CHAR

1234.56\0

SQL_INTEGER

-

1234

01004

SQL_C_CHAR

12345678912\0

SQL_INTEGER

-

-

22003

SQL_C_CHAR

abcdef\0

SQL_INTEGER

-

-

22005

SQL_C_CHAR

abcdef\0

SQL_SMALLINT

-

-

22005

SQL_C_CHAR

1234.56\0

SQL_FLOAT

-

1234.56

N/A

SQL_C_CHAR

1234.5678\0

SQL_FLOAT

-

1234.5678

N/A

SQL_C_CHAR

1.23456e+4\0

SQL_FLOAT

-

1234.56

N/A

SQL_C_CHAR

abcdef\0

SQL_FLOAT

-

-

22005

SQL_C_CHAR

 

SQL_DOUBLE

same as SQL_FLOAT

SQL_C_CHAR

666768696A6b\0

SQL_BINARY

6

BCDEFG

N/A

SQL_C_CHAR

666768696A6\0

SQL_BINARY

6

BCDEF

N/A

SQL_C_CHAR

666768696A6b\0

SQL_BINARY

5

BCDEF

01004

SQL_C_CHAR

HHKKLLMM\0

SQL_BINARY

6

-

22005

SQL_C_CHAR

1995-11-29\0

SQL_DATE

-

1995-11-29

N/A

SQL_C_CHAR

1995-11-29 00:00:00.00\0

SQL_DATE

-

1995-11-29

N/A

SQL_C_CHAR

1995-11-29 23:54:38.23\0

SQL_DATE

-

1995-11-29

01004

SQL_C_CHAR

1995/22/33

SQL_DATE

-

-

22008

SQL_C_CHAR

17:18:19.123

SQL_TIME

-

17:18:19.123

N/A (25)

SQL_C_CHAR

1995-11-29 17:18:19.123

SQL_TIMESTAMP

-

1995-11-29 17:18:19.123

N/A

SQL_C_CHAR

homework\0

SQL_FILE

9

abcdefg

N/A (26)

SQL_C_SHORT

7890

SQL_SMALLINT

-

7890

N/A

SQL_C_SSHORT

7890

SQL_VARCHAR

-

-

S1C00

SQL_C_SSHORT

7890

SQL_BINARY

-

-

07006

SQL_C_SSHORT

7890

SQL_SMALLINT

-

7890

N/A (27)

SQL_C_SSHORT

7890

SQL_INTEGER

-

7890

N/A

SQL_C_USHORT

7890

SQL_INTEGER

-

7890

N/A

SQL_C_LONG

7890

SQL_INTEGER

-

7890

N/A

SQL_C_SLONG

7890

SQL_INTEGER

-

7890

N/A

SQL_C_ULONG

7890

SQL_INTEGER

-

7890

N/A

SQL_C_FLOAT

1234.00

SQL_INTEGER

-

1234

N/A

SQL_C_FLOAT

1234.56

SQL_INTEGER

-

1234

01004

SQL_C_FLOAT

1234567.24

SQL_SMALLINT

-

-

22003

SQL_C_FLOAT

1234.56

SQL_DECIMAL

6,2

1234.56

N/A

SQL_C_FLOAT

1234.56

SQL_DECIMAL

5,1

1234.5

01004

SQL_C_FLOAT

1234.56

SQL_DECIMAL

6,3

-

22003

SQL_C_DOUBLE

same as SQL_C_FLOAT

       

SQL_C_BINARY

ABCDEF

SQL_BINARY

6

ABCDEF

N/A

SQL_C_BINARY

ABCDEF

SQL_BINARY

5

ABCDE

01004

SQL_C_DATE

1996,3,8

SQL_DATE

-

1996,3,8

N/A

SQL_C_DATE

1996,3,8

SQL_TIME

-

-

07006 (28)

SQL_C_DATE

1996,3,8

SQL_TIMESTAMP

-

1996,3,8, 0,0,0,0

N/A (29)

SQL_C_TIME

13,14,15

SQL_DATE

-

-

07006 (30)

SQL_C_TIME

13,14,15

SQL_TIME

-

13,14,15

N/A

SQL_C_TIME

13,14,15

SQL_TIMESTAMP

-

1996,3,8, 13,14,15

N/A (31)

SQL_C_TIMESTAMP

1996,3,8, 13,14,15, 160000000

SQL_DATE

-

1996,3,8

01004 (32)

SQL_C_TIMESTAMP

1996,3,8, 13,14,15, 160000000

SQL_TIME

-

13,14,15

01004 (33)

SQL_C_TIMESTAMP

1996,3,8, 13,14,15, 160000000

SQL_TIMESTAMP

-

1996,3,8, 13,14,15, 160000000

N/A

SQL_C_TIMESTAMP

1996,3,8, 13,14,15, 167800000

SQL_TIMESTAMP

-

1996,3,8, 13,14,15, 167000000

01004 (34)

SQL_C_FILE

homework\0

SQL_LONGVARCHAR

6

abcdefg

N/A (35)

Footnotes

1 p (precision) is the total number of digits and s (scale) is the number of digits right of the decimal point.

2 Slight differences exist between the DBMaker and ODBC 2.0 definition of this data type.

3 An extended data type supported by DBMaker and not included in ODBC 2.0.

4 Valid year values are in the range 1 to 9999.

5 Valid month values are in the range 1 to 12.

6 Valid day values are in the range 1 to the number of days in the month.

7 Valid hour values are in the range 0 to 23.

8 Valid minute values are in the range 0 to 59.

9 Valid second values are in the range 0 to 59.

10 Valid fraction values are in the range 0 to 999,999,999. For example, 500,000,000 represents a half-second. 1,000,000 is a thousandth of a second. 1,000 is a millionth of a second (a microsecond). 1 is a billionth of a second (a nanosecond).

11 Slight differences exist between the DBMaker and ODBC 2.0 definition of this data type.

12 The precision of date, time and timestamp are all defined as the same as their lagrest display size, which are the maximum length of date, time and timestamp format corespondently (dd/mon/yyyy, hh:mm:ss.nnn tt, dd/mon/yyyy hh:mm:ss.nnn tt)

13 The scale of time and timestamp are 3. It means the number of digits in fractional part which could be precisely accepted by DBMaker.

14 Slight differences exist between the DBMaker and ODBC 2.0 definition of this data type.

15 the output data and returned code are according to the date output format set by the user. If the output format for date is yyyy-mm-dd, then the size of user buffer must be at least 11 bytes, otherwise ERROR will be returned.

16 the output data and returned code are according to the date output format set by the user. If the output format for the date is mm-dd-yy, then the size of the user buffer must be at least 9 bytes, otherwise ERROR will be returned.

17 The output data and returned code are according to the time output format setted by user. If the time output format specifies the fractional part, ex: hh:mm:ss.fff, and user buffer is not large enough for put the fractional digits, then wanring message of data truncated will be returned.

18 If the time output format contains second field, ex: hh:mm:ss, but the user buffer is not large enough to carry the second value, ERROR will be returned.

19 suppose user specify the time output format as hh tt, even though minute and second data is lost, no error will be returned because user only want the information for hour.

20 same reason as 1

21 same reason as 2

22 The rule for timestamp format combines both the date format rulls and time format rulls above. So, if the date output format is mm/dd/yy and time output format is hh:mm:ss, then the buffer size must be at least 18 (8 + 1 + 8 + 1), otherwise error will be returned.

23 suppose a column type is FILE, use SQL_C_CHAR to bind this column will fetch the contents of this file into the user buffer, ex: abcdefg is the contents of a file called 'homework', then abcdefg will be putted to the user buffer.

24 the same example as 7, but use SQL_C_FILE to bind this column will fetch the file contents into the new file (ex: student) which is specified by the user buffer.

25 according to the date input format

26 If there is one file of the server site named 'homework' then use SQL_C_CHAR to bind parameter will let the specified FILE column link to the file 'homework'.

27 SQL_C_SSHORT, SQL_C_SHORT, SQL_C_SLONG, SQL_C_LONG, SQL_C_FLOAT and SQL_C_DOUBLE are numeric ODBC C data types

28 SQL_C_DATE cannot be converted to SQL_TIME.

29 When converting SQL_C_DATE to SQL_TIMESTAMP, the time portion of the timestamp is set to zero.

30 SQL_C_TIME cannot be converted to SQL_DATE.

31 The date part of timestamp will be set to the current date when converting SQL_TIME to SQL_C_TIMESTAMP.

32 If the time portion is not zero, data truncated warning will be returned when converting SQL_C_TIMESTAMP to SQL_DATE

33 If the fractional part is not zero, data truncated warning will be returned when converting SQL_C_TIMESTAMP to SQL_TIME

34 Because only 3 digits after decimial in the fractional portion of the timestamp are assured, warning message will be returned when more than 3 digits after decimal.

35 If there is one file called 'homework' on the client site, and its contents are abcdefg, then use SQL_C_FILE to bind parameter will insert its contents into the BLOB or FILE column.

Previous PageTop Of PageTable Of ContentsNext Page

Copyright 2002 SYSCOM Computer Engineering Co. All rights reserved.