Examples of using decimal floating point data in an ODBC application

The DECFLOAT(16) and DECFLOAT(34) SQL data types map to the SQLDECIMAL64 and SQLDECIMAL128 C types. You must convert the character data for decimal floating point values to a decimal floating point type before you can assign the data to DECFLOAT columns. You can convert the data that you retrieve from DECFLOAT columns to other data types for manipulation in an application.

The following code demonstrates how to assign decimal floating point data to DECFLOAT columns. The example:
  • Assigns string constants that represent decimal floating point values to character variables.
  • Converts the character variables to SQLDECIMAL64 and SQLDECIMAL128 format, and assigns the results to SQLDECIMAL64 and SQLDECIMAL128 variables.
  • Binds the SQLDECIMAL64 variable to a DECFLOAT(16) parameter marker, and binds the SQLDECIMAL128 variable to a DECFLOAT(34) parameter marker.
/* Declare variables for decimal floating point data */
SQLDECIMAL64	 H1DFP16;
SQLDECIMAL128 H1DFP34;

SQLINTEGER		LEN_H1DFP16;
SQLINTEGER   LEN_H1DFP34;

SQLCHAR      H1CHAR[100];
decContext_t l_decCtxt64; 
decContext_t l_decCtxt128;

/* Initialize a decContext structure to default values */
decContextDefault(&l_decCtxt64, DEC_INIT_DECIMAL64);  
decContextDefault(&l_decCtxt128, DEC_INIT_DECIMAL128);

/* Convert a character string to decimal64 format for insert */
strcpy( (char *)H1CHAR, "6400E-2" );        
decimal64FromString((decimal64_t *)&H1DFP16,
                    (char *)H1CHAR,         
                    &l_decCtxt64);          
LEN_H1DFP16 = sizeof(H1DFP16);     

/* Convert a character string to decimal128 format for insert */
strcpy( (char *)H1CHAR, "1.28E2" );           
decimal128FromString((decimal128_t *)&H1DFP34,
                     (char *)H1CHAR,          
                     &l_decCtxt128);          
LEN_H1DFP34 = sizeof(H1DFP34);                         

/* Bind to DECFLOAT(16)*/
SQLParameter( hstmt,
		  1,
		  SQL_PARAM_INPUT,
		  SQL_C_DECIMAL64,
	        SQL_DECFLOAT,
              16,
              0,
		  (SQLPOINTER)&H1DFP16,
		  sizeof(H1DFP16),
		  (SQLINTEGER *)&LEN_H1DFP16);

/* Bind to DECFLOAT(34)*/
SQLParameter( hstmt,
		  1,
		  SQL_PARAM_INPUT,
		  SQL_C_DECIMAL128,
	        SQL_DECFLOAT,
              34,
              0,
		  (SQLPOINTER)&H1DFP34,
		  sizeof(H1DFP34),
		  (SQLINTEGER *)&LEN_H1DFP34);
The following code demonstrates how to retrieve decimal floating point data from DECFLOAT columns. The example:
  • Binds a DECFLOAT(16) column to an SQLDECIMAL64 variable, and binds a DECFLOAT(34) column to an SQLDECIMAL128 variable.
  • Retrieves the data from the DECFLOAT columns into the SQLDECIMAL64 and SQLDECIMAL128 variables.
  • Converts the SQLDECIMAL64 and SQLDECIMAL128 variables to character format, and assigns the results to character variables.
/* Declare variables for decimal floating point data */
SQLDECIMAL64	 H1DFP16;
SQLDECIMAL128 H1DFP34;

SQLINTEGER		 LEN_H1DFP16;
SQLINTEGER    LEN_H1DFP34;

SQLCHAR       H1CHAR[100];
decNumber_t   tempDecNum;


/* Bind DECFLOAT(16) column */
rc = SQLBindCol( hstmt,
	     	     1,
		     SQL_C_DECIMAL64,
		     (SQLPOINTER)&H1DFP16,
		     sizeof(H1DFP16),
		     (SQLINTEGER *)&LEN_H1DFP16);

/* Bind DECFLOAT(34) column */
rc = SQLBindCol( hstmt,
	           1,
		     SQL_C_DECIMAL128,
		     (SQLPOINTER)&H1DFP34,
		     sizeof(H1DFP34),
		     (SQLINTEGER *)&LEN_H1DFP34);

rc = SQLFetch( hstmt );

/* Convert H1DFP16 to a character string for display */
decimal64ToString( (decimal64_t *)&H1DFP16, (char *)H1CHAR );

/* Convert H1DFP34 to decNumber form in preparation for arithmetic or other operations */
decimal128ToNumber( (decimal128_t *)&H1DFP34, &tempDecNum);