Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Data type conversion

Former Member
0 Likes
4,513

Hi all,

How can convert a quantity field with data element BDMNG (data type QUAN13) to data element MNGC6 (data type CHAR10) or vice versa? THanks in advance.

4 REPLIES 4
Read only

Former Member
0 Likes
1,512

u don't need to do any thing explicitely. just define variables for both types.

SAP will do automaticaly.

Sameer

Read only

Former Member
0 Likes
1,512

Type Conversions

Every time you assign a data object to a variable, the data types involved must either be compatible , that is, their technical attributes (data type, field length, number of decimal places) must be identical, or the data type of the source field must be convertible into the data type of the target field.

In ABAP, two non-compatible data types can be converted to each other if a corresponding conversion rule exists. If data types are compatible, no conversion rule is necessary.

If you use the MOVE statement to transfer values between non-compatible objects, the value of the source object is always converted into the data type of the target object. With all ABAP operations that perform value assignments between data objects (for example, arithmetic operations or filling internal tables), the system handles all the necessary type conversions as for the MOVE statement. If you try to assign values between two data types for which no conversion rule exists, a syntax error or runtime error occurs.

The following sections contain the conversion rules for incompatible ABAP data types:

Conversion Rules for Elementary Data Types

Conversion Rules for References

Conversion Rules for Structures

Conversion Rules for Internal Tables

With some ABAP statements that pass data between different objects, the alignment of the data objects is also important.

Alignment of Data Objects

Conversion Rules for References

ABAP currently uses class and interface variables within ABAP Objects. Both are pointers to objects. You can assign values to them in the following combinations:

If the two class references are incompatible, the class of the target field must be the predefined empty class OBJECT.

When you assign a class reference to an interface reference, the class of the source field must implement the interface of the target field.

If two interface references are incompatible, the interface of the target field must contain the interface of the source field as a component.

When you assign an interface reference to a class reference, the class of the source field must be the predefined empty class OBJECT.

Conversion Rules for Structures

ABAP has one rule for converting structures that do not contain internal tables as components. There are no conversion rules for structures that contain internal tables. You can only make assignments between structures that are compatible.

You can combine convertible structures in the following combinations:

Converting a structure into a non-compatible structure

Converting elementary fields into structures

Converting structures into elementary fields

In each case, the system first converts all the structures concerned to type C fields and then performs the conversion between the two resulting elementary fields. The length of the type C fields is the sum of the lengths of the structure components. This rule applies to all operations using structures that do not contain internal tables.

If a structure is aligned, the filler fields are also added to the length of the type C field.

A non-aligned structure without filler fields:

If you convert a structure into a shorter structure, the original structure is truncated. If you convert a structure into a longer one, the parts at the end are not initialized according to their type, but filled with blanks.

It can make sense to assign a structure to another, incompatible, structure if, for example, the target structure is shorter than the source, and both structures have the same construction over the length of the shorter structure. However, numeric components of structures that are filled in incompatible assignments may contain nonsensical or invalid values that may cause runtime errors.

DATA: BEGIN OF FS1,

INT TYPE I VALUE 5,

PACK TYPE P DECIMALS 2 VALUE ‘2.26’,

TEXT(10) TYPE C VALUE ‘Fine text’,

FLOAT TYPE F VALUE ‘1.234e+05’,

DATA TYPE D VALUE ‘19950916’,

END OF FS1.

DATA: BEGIN OF FS2,

INT TYPE I VALUE 3,

PACK TYPE P DECIMALS 2 VALUE ‘72.34’,

TEXT(5) TYPE C VALUE ‘Hello’,

END OF FS2.

WRITE: / FS1-INT, FS1-PACK; FS1-TEXT, FS1-FLOAT, FS1-DATE.

WRITE: / FS2-INT, FS2-PACK, FS2-TEXT.

MOVE FS1 TO FS2.

WRITE: / FS2-INT, FS2-PACK, FS2-TEXT.

Output:

5 2.26 Fine text 1.234000000000000E+05 09161995

3 72.34 Hello

5 2.26 Fine

This example defines two different structures, FS1 and FS2. In each one, the first two components have the same data type. After assigning FS1 to FS2, only the result for the first two components is as if they had been moved component-by-component. FS2-TEXT is filled with the first five characters of FS1-TEXT. All other positions of FS1 are omitted.

Conversion Rules for Internal Tables

Internal tables can only be converted into other internal tables. You cannot convert them into structures or elementary fields.

Internal tables are convertible if their line types are convertible. The convertibility of internal tables does not depend on the number of lines.

Conversion rules for internal tables:

Internal tables which have internal tables as their line type are convertible if the internal tables which define the line types are convertible.

Internal tables which have line types that are structures with internal tables as components are convertible according to the conversion rules for structures if the structures are compatible.

Alignment of Data Objects

Elementary fields with types I and F occupy special memory addresses that are platform-specific. For example, the address of a type I field must be divisible by 4, and the address of a type F field by 8. Consequently, type I and F fields are known as aligned fields. Structures containing fields with type I or F are also aligned, and may contain filler fields immediately before their aligned components.

The system normally aligns fields and structures automatically when you declare them.

You must take alignment into account in the following cases:

When you pass elementary fields or structures to a procedure as actual parameters where the corresponding formal parameter is not typed accordingly.

When you declare field symbols

When you use a work area with an ABAP Open SQL statement that does not have the same type as the database table as defined in the ABAP Dictionary.

When you process components of structures.

http://help.sap.com/saphelp_nw04/helpdata/en/cf/21f2e5446011d189700000e8322d00/content.htm

Message was edited by:

Karthikeyan Pandurangan

Read only

former_member480923
Active Contributor
0 Likes
1,512

Hi Please check the following code



DATA: w_bdmng TYPE bdmng VALUE '13.335',
      w_mngc6 TYPE mngc6,
      w_mngc7 TYPE mngc6,
      w_bdmng1 TYPE bdmng.


WRITE w_bdmng TO w_mngc6.

w_mngc7 = w_mngc6.

SEARCH w_mngc7 FOR ','.

IF sy-subrc = 0.
  REPLACE ALL OCCURRENCES OF ',' IN w_mngc7 WITH '.'.
  CONDENSE w_mngc7.
  MOVE w_mngc7 TO w_bdmng1.
ENDIF.

WRITE: w_mngc6, w_bdmng1.

Hope that Helps

Anirban M.

Read only

Former Member
0 Likes
1,512

Hi

if u don't use for all entries

then u define these feilds of same type and length

like MNGC6(10) type c

BDMNG(10) type c

reward if useful