Skip to Content
author's profile photo Former Member
Former Member

Replace First Character with Space

In BW I have a record coming in and wtihin the address field the first character contains a ! or #. This is causing the DSO not to activate due to invalid characters of ! or # as the first character. I am trying to replace these characters with a space. Any ideas? Listed below I am trying to to determine if the source field contains a ! as the first character. If this is true then I am replacing ! with a space in the REPLACE statement. Still no luck so asking around for suggestions. Thanks.

DATA: VADDRESS LIKE SOURCE_FIELDS-ADDRESS.

IF SOURCE_FIELDS-ADDRESS CP '!'.

VADDRESS = SOURCE_FIELDS-ADDRESS.

REPLACE '!' WITH ' ' INTO VADDRESS.

RESULT = VADDRESS.

ELSE.

RESULT = SOURCE_FIELDS-ADDRESS.

ENDIF.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

9 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Nov 09, 2012 at 04:23 PM

    The fix was created a FORM globally that replaces any hexidecimal characters 00-1F, which are not valid and also replacing the ! and # in the first field of the character.

    FORM

    *$*$ begin of 2nd part global - insert your code only below this line *

    FORM format_for_bw USING i_replace TYPE string

    CHANGING c_text_field TYPE c.

    DATA: l_field_length TYPE i,

    l_index TYPE sy-index.

    FIELD-SYMBOLS <l_char> TYPE X.

    *Hexadecimal character OO-1F are not valid in BW and therefore need to

    *be replaced.

    l_field_length = STRLEN( c_text_field ).

    DO l_field_length TIMES.

    *Retrieve the character's hex value

    l_index = sy-index - 1.

    assign c_text_field+l_index(1) to <l_char> casting.

    *If the hex value is between 00 and 1F, replace with space

    IF <l_char> <= '1F'.

    c_text_field+l_index(1) = i_replace.

    ENDIF.

    ENDDO.

    * Need to check the source field for any characters BW cannot handle

    * ! as the first character, # in any position

    IF c_text_field(1) = '!'.

    SHIFT c_text_field LEFT DELETING LEADING '!'.

    ELSEIF c_text_field(1) = '#'.

    SHIFT c_text_field LEFT DELETING LEADING '#'.

    ENDIF.

    ENDFORM.

    Next for each text field that would have the potental of invalid characters apply the code below, which calls the FORM for cleanup of the characters.

    RESULT = SOURCE_FIELDS-LASTNAME.

    PERFORM format_for_bw USING SPACE

    CHANGING RESULT.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 19, 2012 at 01:03 PM

    Hi Mary Jo,

    You mentioned that the first character may contain a ! or a #. The pound character may not be an actual pound character but SAP's representation of a non-printing character, e.g., a tab. We've seen this issue when a user copies-and-pastes text from another document and unintentionally brings along non-printing characters.

    We corrected the issue by running any field that may contain non-printing characters (in your case, the address field) through a subroutine to remove any invalid characters.

    For your scenario, you could construct a string of valid characters, then check if vaddress begins with anything other than this set of valid characters (IF vaddress(1) NOT CO valid_characters) and clear vaddress(1) if non-valid characters are found.

    Cheers,

    Amy

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Please be certain that the character "!" is really hex 0021 and not something else. Your description of the behavior seems to point to that character being something else, but is only displaying the "!". You can see this in the debugger. Also you might try simply replacing it with "!" by typing over the character in debugger.

  • author's profile photo Former Member
    Former Member
    Posted on Sep 19, 2012 at 09:09 AM

    fieldname(1) = ' '.

    will work too if the field is not Type string.

    Regards Nico

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 19, 2012 at 05:04 AM

    you can use REPLACE FIRST OCCURRENCE

    but if u got invalid character error then you can use T.CODE RSKC you pass

    @! $~{}_-|[]`\# and execute after that u active your DSO

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 19, 2012 at 05:15 AM

    Hi Mary,

    Please use the below code.

    REPLACE FIRST OCCURRENCE OF '!' SOURCE_FIELDS-ADDRESS WITH ''.

    CONDENSE SOURCE_FIELDS-ADDRESS.

    Hope its useful.

    Regards,

    Rajesh

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks for responding. One thing to note not every field will begin with the character ! so I my intent was to search the character string for a ! and then if found do the REPLACE statement. With the below code even though it doesn't say REPLACE FIRST OCCURRENCE it is still only replacing the first find. But for some reason the string search is not recognizing the ! as a character so it doesn't find it. My code works if I switch it to a 1 for the search. See below.

      Code

      IF SOURCE_FIELDS-ADDRESS CA '1'

      VADDR = SOURCE_FIELDS-ADDRESS.

      REPLACE '1' WITH ' ' INTO VADDR1.

      RESULT = VADDR1.

      Here are the results from the test.

      Source field: 115 WEST COLLEGE DRIVE1

      Result: 15 WEST COLLEGE DRIVE1

  • author's profile photo Former Member
    Former Member
    Posted on Sep 19, 2012 at 08:19 AM

    Hi,

    Try to use TRANSLATE command.

    vaddress = '!jake#jason'.

    TRANSLATE vaddress USING '! # '.

    WRITE:/ vaddress.

    Regards,

    Jake

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks for responding. I tried you suggestion and it appears to only work if the ! is contained within the string but not as the first character. One thing to note is not every field will have a ! as the first character so that is why I am applying the search string statement first and if an ! is found then apply the TRANSLATE option. Plus it appears to replace every character of ! where my intent is to just replace the first character. Below is my code and the results. Thanks.

      CODE

      IF SOURCE_FIELDS-CANDIDATECITY CA '!'.

      VADDR2 = SOURCE_FIELDS-CANDIDATECITY.

      TRANSLATE VADDR2 USING '! '.

      WRITE:/ VADDR2.

      RESULT = VADDR2.

      TEST

      Source: 115! WEST! COLLEGE

      Result: 115 WEST COLLEGE

  • Posted on Sep 19, 2012 at 01:58 PM

    You can get the list of allowed characters in the table RSALLOWEDCHAR.

    (Table managed by the transaction RSKC reported by Krupa)

    Thus, you can remove / replace illegal characters in the string or char fields with simple ABAP statements, but beware of the risk of generating duplicate keys!

    Regards,

    Raymond

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 19, 2012 at 05:50 PM

    We too had a problem with strange characters in files coming from external sources( I believe it was an excel SS file that sometimes had some really odd characters) We found just the FM to call to fix them. Have a look at function module SCP_REPLACE_STRANGE_CHARS. It does exactly what its name says. Check out the Function Module Documentation for all the gory details. (It is one of the few SAP FMs that actually has some English documentation!)

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 19, 2012 at 10:25 PM

    FORM replace_invalid_first_with_space changing cv_text type clike.

    STATICS:

    lv_invalid TYPE STRING.

    IF lv_invalid is initial.

    concatenate

    '!'

    cl_abap_char_utilities=>horizontal_tab "control characters visible as #

    cl_abap_char_utilities=>cr_lf

    cl_abap_char_utilities=>new_line

    * add more invalid characters

    INTO lv_invalid.

    ENDIF

    IF strlen( cv_text ) > 0.

    IF lv_invalid CA cv_text(1).

    cv_text(1) = space.

    ENDIF.

    ENDIF.

    ENDFORM.

    Regards

    Clemens

    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.