วันพฤหัสบดีที่ 13 มีนาคม พ.ศ. 2551

Converting character fields to numeric fields with SQL

CONVERTING CHARACTER FIELDS TO NUMERIC FIELDS WITH SQL
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

ไม่มีความคิดเห็น: