Text Functions (LEFT, RIGHT, MID, FIND, TRIM, SUBSTITUTE)
Text Functions: Clean and Extract Business Data
Imported customer names, product codes and branch labels often combine useful information with inconsistent spacing or separators. Excel's text functions are built-in operations that extract, locate or change characters in a text value. You use them to turn mixed labels into consistent fields while preserving the original records in a separate column.
Choose the operation you need
Start by deciding whether you need characters, a character position or a cleaned value. These are different outputs. LEFT, RIGHT and MID return text, even when the result contains only digits. FIND returns a number representing a position.
A function's arguments appear inside parentheses. Enter a formula with an equals sign, put literal text inside double quotation marks and leave cell references unquoted. The examples use commas to separate arguments.
Function and syntax | Main purpose |
|---|---|
LEFT(text, num_chars) | Take a specified number of characters from the beginning. |
RIGHT(text, num_chars) | Take a specified number of characters from the end. |
MID(text, start_num, num_chars) | Take characters beginning at a specified position. |
FIND(find_text, within_text, start_num) | Locate the first matching text at or after the search start. |
TRIM(text) | Remove outer ordinary spaces and compress repeated internal ordinary spaces. |
SUBSTITUTE(text, old_text, new_text, instance_num) | Replace matching text throughout a value or at one occurrence. |
Character positions begin at 1. Spaces and punctuation count. In LEFT and RIGHT, omitting num_chars requests one character. In FIND, omitting start_num begins the search at position 1. In SUBSTITUTE, omitting instance_num replaces every matching occurrence; supplying 2 replaces only the second. These optional inputs need not be typed when you want their default behaviour.
Extract using positions and boundaries
Example: A2 contains PD:582:WEB. The formula =LEFT(A2,2) returns PD, =MID(A2,4,3) returns 582, and =RIGHT(A2,3) returns WEB. MID's final argument is a character count, not an ending position.
A delimiter is a character or sequence that separates fields, such as the colon in this code. When field lengths vary, locate the delimiter rather than guessing a fixed width. Here, =FIND(":",A2) returns 3, so =LEFT(A2,FIND(":",A2)-1) extracts the field before it.
For a single-character delimiter at position p, the governing boundary rules are:
FIND is case-sensitive. A missing match returns #VALUE!, not zero. Its search start is inclusive, and its result always counts from the beginning of the original text. To locate another occurrence, begin after the previous match.
Clean text before extracting it
TRIM removes leading and trailing ordinary spaces and reduces repeated internal ordinary spaces to one. It preserves the single spaces between words. To remove every ordinary space instead, use =SUBSTITUTE(A2," ",""); the empty pair of quotation marks supplies no replacement characters.
SUBSTITUTE matches literal text and respects letter case. For example, =SUBSTITUTE("INV/54/REV","/","-",2) returns INV/54-REV. Leaving off the final argument changes both slashes.
When cleanup changes character positions, locate and extract from the same cleaned value. A separate cell makes this easy to inspect. The diagram shows a useful order when both cleaning and extraction are needed; a record may need only some of these operations.
What to remember
- Choose LEFT or RIGHT for fixed widths and MID for a specified start and count.
- Use FIND to locate a boundary, with positions starting at 1.
- Use TRIM for ordinary spacing and SUBSTITUTE for literal replacements.
- Check the cleaned text and preserve meaningful leading zeros.
Example: Preparing an Online Order Code
A retailer receives the exact text " NTH/0063/ONLINE " in A2, excluding the quotation marks. The value has two ordinary spaces at each end. Slashes separate the branch, a four-character order identifier and the sales channel. The branch length can vary across records, but every order identifier contains exactly four digits. In this import, every channel is ONLINE.
a) Write one formula in B2 that changes every slash to a hyphen and removes the outer spaces. State the cleaned value.
A nested formula places one function inside another, allowing the inner result to become the outer function's input.
- Count the separators. There are ________ slashes to replace.
- Use SUBSTITUTE(A2,"/","-") to replace every slash. Omitting the occurrence argument changes both separators. This operation leaves the outer spaces in place.
- Apply TRIM to that result with =TRIM(SUBSTITUTE(A2,"/","-")).
The formula in B2 is =TRIM(SUBSTITUTE(A2,"/","-")), and its value is NTH-0063-ONLINE. Keep A2 as the original imported record.
b) Using B2, write a formula that extracts the branch even when its length varies. State the branch for this record.
- Locate the first hyphen with =FIND("-",B2). Its position in the cleaned value is ________.
- Subtract one to exclude the separator. The branch contains ________ characters in this record.
- Supply that calculated count to LEFT: =LEFT(B2,FIND("-",B2)-1).
The branch is NTH. The formula adapts to other branch lengths because it locates the first hyphen rather than assuming that the branch always has three characters.
c) Using B2, extract the four-character order identifier. State the formula and result.
- Find the first hyphen in B2, at position 4.
- Begin one position later. The identifier starts at position ________.
- Request the specified identifier width of ________ characters with =MID(B2,FIND("-",B2)+1,4).
The order identifier is 0063. MID returns text, so the leading zeros remain part of the identifier. This field identifies an order; it is not a quantity that needs arithmetic.
d) Using B2, extract the channel. State the formula and result.
- The stated channel ONLINE contains ________ characters and occupies the end of the cleaned value.
- Use RIGHT to request those final characters: =RIGHT(B2,6).
- Confirm that the returned text excludes the preceding hyphen.
The channel is ONLINE. The fixed count works because the import explicitly uses this same six-character channel for every record. A different import with variable channel lengths would need its boundary located instead.
An inventory worksheet stores the exact text TX-BX7-WEST in A2, with no spaces. Complete the table with each formula's exact result. Enter text without quotation marks and positions as integers.
| Formula | Result |
| =LEFT(A2,2) | |
| =RIGHT(A2,4) | |
| =MID(A2,4,3) | |
| =FIND("-",A2) | |
| =FIND("-",A2,4) |
A sales worksheet stores a territory name followed by one hyphen and a three-digit account suffix. Territory names vary in length and contain no hyphens or spaces. A2 currently contains REGIONAL-629. Which formula extracts the full territory name and also works for the other records?