Knowledge Base |

# INFO: Transposing First Character w/ Last Character in a Field

Article ID: 114498

Article Last Modified on 3/3/2005

APPLIES TO

- Microsoft Visual FoxPro 3.0 Standard Edition
- Microsoft Visual FoxPro 5.0 Standard Edition
- Microsoft Visual FoxPro 6.0 Professional Edition
- Microsoft FoxPro 2.0
- Microsoft FoxPro 2.6a Professional Edition for Macintosh

This article was previously published under Q114498

## SUMMARY

The code in this article describes how to switch the first character with the last character in a field for all records.

## MORE INFORMATION

The following code demonstrates an easy way to do this:

    * Replaces the field for each record
     REPLACE <fieldname> WITH RIGHT(TRIM(<fieldname>),1)+;
         SUBSTR(<fieldname>,2,LEN(TRIM(<fieldname>))-2)+LEFT(<fieldname>,1) ;
         FOR LEN(TRIM(<fieldname>))>1

Before the commands are executed, a sample table may look similar to the following:

       <FIELDNAME>
       -----------
       A-0000-Z
       B-0-Y
       C-000000-X
       D-00000-W

After the commands are executed, the table looks as follows:

       <FIELDNAME>
       -----------
       Z-0000-A
       Y-0-B
       X-000000-C
       W-00000-D

The REPLACE command concatenates the original rightmost character with the a substring of the original value, which omitts the first and last characters, and appends the original leftmost character to that string.
This command works with variable-length character fields. With a few modifications, this code can be quite flexible and useful for string- manipulation purposes and can be adapted to work with other types of fields.

## REFERENCES

"Commands & Functions," version 2.0 (MS-DOS), pages C3-701 to C3-704, and C3-659 to C3-660
"Language Reference," version 2.5 (MS-DOS and Windows), pages L3-862 to L3-866, and L3-816 to L3-818

Additional query words: FoxMac FoxDos FoxWin VFoxWin 2.50 2.50a 2.50b 2.50c 2.60 change reverse alter

Keywords: kbinfo KB114498