Converting Character Fields to Numeric Fields with SQL

Article ID: 14822

One question that arises frequently is how to convert a character field to a numeric field without use the Move op-code. In past issues we've discussed APIs that perform the conversion. You might be interested in knowing that SQL's Cast statement lets you change a field's data type. With embedded SQL, you can therefore convert a character field value to a numeric field. The following code excerpt initializes character field CharFld to the value '123.45' and uses Cast to convert it to numeric field NbrFld.

      *    *****************************************************************
      *    * Sample code demonstrating SQL Cast to convert character field *
      *    * to numeric field                                              *
      *    *****************************************************************

     D CharFld         S              6    Inz( '123.45' )
     D NbrFld          S              5S 2

C/Exec SQL
C+   Set :NbrFld = Cast( :CharFld as Dec( 5 , 2 ) )
C/End-Exec

ProVIP Sponsors

ProVIP Sponsors