Binary and decimal datatypes

The following sections contain additional information about binary and decimal datatypes.


TDSVARYBIN

Use TDSVARYBIN rather than TDSVARBINARY. DBVARYBIN and TDSVARYBIN objects also include the “LL” length specification that precedes the data.

NoteIn Client-Library, the CS_VARBINARY datatype includes the “LL” length specifications and therefore can be mapped to TDSVARYBIN.


Converting Sybase decimal and numeric data

Use TDSNUMERIC and TDS_SYBASE_DECIMAL datatypes for Sybase Adaptive Server numeric and decimal data. These datatypes are defined as:

01 NUMDEC
    05 PRECISION CHAR X(1).
    05 SCALE 	    CHAR X(1).
    05 ARR 	      CHAR X(33).

In the preceding example, 1 byte is for precision, 1 byte is for scale, and 33 bytes are for the packed value.

You can use conversion between these datatypes and character data. Open ServerConnect also supports conversion between these datatypes and TDS_PACKED_DECIMAL (IBM packed decimal).


Converting packed decimal data

You can convert TDS_PACKED_DECIMAL to TDSNUMERIC, TDS_SYBASE_DECIMAL, character, float and money datatypes.


Converting packed decimal to character data

When converting TDS_PACKED_DECIMAL data to character datatypes, you must adjust the length of the result variable.

Use this formula to set the unpacked length:

	Result Length =  (2 * Source Length) - 1.

When converting to character datatypes, automatic conversions may add a sign, a decimal point, and leading or trailing zeros. Allow one byte each for the sign and decimal point, and enough bytes to allow for the leading and trailing zeros.

When converting from packed decimal to character datatypes, Gateway-Library functions add zeros to the left of the decimal point for fractional values and to the right of the decimal point for integers. If no decimal point is present, one is added.

For all values, start with the defined length (precision):

For integer values:

For fractional values: (n < 1 and > -1, precision = scale):

For non-integer values greater than 1:

In the “Calculation” column in Table 2-10, P=precision; s=sign; d=decimal point; z=zero(s). In the Character-Type Result column, b=blank.

Table 2-10: Examples of decimal-to-character conversions

Decimal value

Precision

Scale

Calculation P+s+d+z = result length

Character-type result

Result length

 1
3
0
3+1+1+1 = 6
 bbb1.0
6
 123
3
0
3+1+1+1 = 6
 b123.0
6
-123
3
0
3+1+1+1 = 6
 -123.0
6
 1.23
5
2
5+1+1+0 = 7
 bbb1.23
7
 .3
5
2
5+1+1+0 = 7
 bbb0.30
7
-.2
5
2
5+1+1+0 = 7
 bb0.20
7
123.45
5
2
5+1+1+0 = 7
 b123.45
7
.123
3
3
3+1+1+1 = 6
 b0.123
6
-.123
3
3
3+1+1+1 = 6
 -0.123
6

For packed decimal-to-character conversions, the low-order digits of the character string are truncated. If the actual result is greater than the length of the destination, the low-order bytes are truncated.

For character-to-packed decimal conversions, the character string is scanned from left to right to determine precision and scale. The resulting packed decimal value contains the highest order digits that fit in the length specified by the destination length.


Packed decimal to numeric, decimal, float, money conversion

You can convert between IBM packed decimal and Sybase numeric, decimal, float or money datatypes.

You can also convert Sybase numeric, decimal, float or money to packed decimal. The result has the same scale as the source.

When converting from packed decimal to Sybase numeric, decimal, float or money, specify 35 as the destination length.


Sybase numeric or decimal to character data conversion

For numeric or decimal to character conversions, the precision and scale of the numeric data item are used to determine the output length of the character string. The source length should be the actual length of the numeric data item. The destination length should be precision + 2. If this length is less than the actual length of the result, TDS_OVERFLOW is returned.

For character to numeric or decimal conversions, the character string is scanned from left to right to determine precision and scale. You must specify the destination length as 35, or TDS_INVALID_LENGTH is returned. The numeric or data item contains the precision and scale as the first two bytes.