Skip to Content

Casting of Data Types

Hi All ,

I want to know if casting of data types is possible in ABAP. I mean if I have declared a datatype as

data : x type char5.

and Now I want to make x as char10.

Is there any method to achieve this?

Thanks and regards

Sujeet

Add a comment
10|10000 characters needed characters exceeded

Related questions

4 Answers

  • Best Answer
    Posted on Dec 17, 2004 at 08:41 AM

    Hi Sujeet

    If your requirement suits, you can use dynamic programming which is a nice feature of ABAP.

    You can use field-symbols (think them of as aliases for fields) or generic variables.

    For more information I recommend the e-class "Advanced" target="_blank">https://www.sdn.sap.com/sdn/elearning.sdn?class=/public/eclasses/teched04/ABAP351.htm">"Advanced and Generic Programming in ABAP".

    As a last thing, let me introduce you the SDN forums pointing system: You can assign points to posts which help you solving your problem. You can reward points by clicking the yellow star icon at header of each post. You can assign:

    - one 10 points (solved)

    - two 6 points (very helpful answer)

    - many 2 points (helpful answer)

    Kind regards...

    *--Serdar

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 17, 2004 at 06:47 AM

    Why don't you use Field-Symbols, you can use it for any type, not just characters and of any length ?

    data: x type char5,
          y type char10.
    FIELD-SYMBOLS : <FS> TYPE ANY.
    assign x to <FS>.
    write : <FS>.
    assign y to <FS>.
    write: <FS>.
    

    Secondly, I don't think it would be possible to cast from a lower length character type to a higher length character type.

    You can do casting in <b>ASSIGN</b>.

    Regards,

    Subramanian V.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 17, 2004 at 06:59 AM

    Hello Sujeet,

    Once you have declared the variable x with the type CHAR5, you cannot change the type of this variable.

    However, you have the option of using a field-symbol. You can declare a generic field symbol and assign to it any data object of any length (or type). Consider this example.

    field-symbols <fs> type any.
    
    data : char1(5) type c value 'SAPAG',
           char2(8) type c value 'SAPINDIA'.
    
    assign char1 to <fs>.
    write <fs>.
    
    assign char2 to <fs>. 
    write <fs>.
    

    Get back if you still have any doubts.

    Regards,

    Anand Mandalika.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 17, 2004 at 08:32 AM

    Casting is possible using field-symbols. There are various options around the ASSIGN command. However casting char5 to char10 is problematic, since the original is smaller than the target representation.

    Kind Regards

    Klaus

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.