3.6 Text Functions: LEFT, RIGHT, MID, LEN, FIND and SEARCH
Read the pieces hidden inside a code
A fictional sales report exports an order ID as BLK-0042. The first three letters describe a platform in this exercise, and the final four characters form a serial label. You might want the platform in its own column so you can group records. You might also want to keep the serial label exactly as it was received, including the two leading zeros.
These sample codes use names inspired by Blinkit and Amazon Now for familiarity. They are invented for this practice lesson. They describe no actual order-ID policy at either company. Begin by looking at the characters before you write a formula. A character is one unit of the text the function counts. In these ASCII examples, each ordinary letter, digit, space and hyphen occupies one position. The first position is one.
Find out what stays consistent
- Fixed width. When every prefix is three characters and every serial label is four, choose the exact number of characters to take from either edge. Use one short and one typical example to check the contract before relying on it.
- Moving boundaries. When a city or platform label varies in length, use a separator to find the relevant positions. A delimiter is a character or series of characters that marks the boundary between fields.
- Meaning of the output. A serial value copied into a new text column can still represent an ID. A number used for arithmetic has a different role. Decide whether preserving its written form matters before you convert it.
Choose an edge, a start or a length
LEFT and RIGHT select from the edges
=LEFT(A2,3) takes the first three characters of the text in A2. If A2 holds BLK-0042, the result is BLK. =RIGHT(A2,4) takes the last four, giving the text 0042. The second argument says how many characters to take, even when the function starts from the right. Omitting that optional argument requests one character. In this exercise, give the number explicitly so someone can read your intended code width.
When you request more characters than the text contains, LEFT or RIGHT can return the whole text. A short or broken code can therefore produce a result without an error. If a source is expected to hold an eight-character ID, check its length in a separate formula before interpreting the output. Returning some text is not proof that the input matched the contract.
MID starts at a numbered character
=MID(A2,5,4) asks Excel to start at position five and take four characters. For the eight-character code, it also gives 0042. The second argument is the starting position and the third is the number of characters. The third argument is a length. Passing an ending position there can pick up later text. For example, in an address code with several fields, starting at five and requesting seven characters gives positions five through eleven.
When the requested start lies past the end of the text, MID can return empty text. When the remaining part is shorter than the requested count, it returns only available characters. Keep the required structure check separate from extraction, so an empty result means what you intend it to mean.
LEN measures the text before you slice it
=LEN(A2) counts the characters in BLK-0042 when A2 contains the ordinary eight-character code with no hidden characters. The hyphen counts, so the result is eight. An accidental space before or after the code also counts. If a source value looks correct but LEN is higher than expected, inspect that source. A search around visible separators or a length comparison after cleaning can reveal unexpected characters.
Extra spaces can be meaningful inside a name. The space in College Road is part of the area label. Treat it as one character when you calculate positions. A cleanup rule should preserve intentional internal spaces, so agree on which data should be trimmed before changing a formula.
| Question | Expression | Expected result |
|---|---|---|
| Read the prefix | =LEFT(A2,3) | BLK |
| Read the serial from the end | =RIGHT(A2,4) | 0042, as text |
| Start at the first serial digit | =MID(A2,5,4) | 0042, as text |
| Check the code length | =LEN(A2) | 8, as a number |
Find a position before returning part of the text
FIND and SEARCH return a number, the position where the matching text begins. They help you work out where MID should start or how many characters LEFT should take. For ASCII codes, you can verify the returned positions by pointing to the character in the original text.
FIND checks case and treats wildcard characters literally
=FIND("-",A2) returns four for BLK-0042. =FIND("BLK",A2) returns one. Lowercase "blk" gives #VALUE! with FIND when only the uppercase code is present. FIND does not use wildcard patterns. Searching for a literal question mark looks for a real question mark.
SEARCH accepts a case-insensitive pattern
=SEARCH("blk",A2) returns one for the same code. SEARCH also recognizes an asterisk for a sequence of characters and a question mark for a single character. In a controlled product label such as Fresh Milk 500 ml, =SEARCH("M?lk",A2) finds the beginning of Milk provided A2 contains that label. The match starts at position seven. If you want an actual question mark instead, use a tilde before it, as in =SEARCH("~?",A2).
Start a second search after the first match
Both functions let you give a start number. Excel begins looking at that position in the original string and still reports the matching position relative to the whole text. To find a second hyphen, first find the first one, then start searching one character later. For a two-letter separator you would advance by its length to avoid testing inside the earlier match. When a required delimiter is missing, the search produces an error. Keep that visibility while learning so you know which assumption failed.
Lab 1 · Split fixed-width order IDs and keep their zeros
Open a fresh scratch tab named Codes. Type Order ID in A1, Platform prefix in B1, Serial text in C1, Hyphen position in D1 and Code length in E1. The codes in this lab are text because they contain letters and a hyphen. They are deliberately uniform before you test an exception.
| A Order ID | B Prefix | C Serial text | D Hyphen position | E Length |
|---|---|---|---|---|
| BLK-0042 | BLK | 0042 | 4 | 8 |
| BLK-0007 | BLK | 0007 | 4 | 8 |
| AMN-0129 | AMN | 0129 | 4 | 8 |
| BLK-1006 | BLK | 1006 | 4 | 8 |
Calculate and reconcile the new columns
- In B2 enter
=LEFT(A2,3). Inspect BLK. Fill it down through B5. Row 4 should show AMN because its source value starts with those letters. - In C2 enter
=RIGHT(A2,4). Inspect 0042 and fill it down. C3 should show 0007. Both results keep four characters, including zeros. In a spare cell, check=ISTEXT(C3)and expect TRUE. - Enter
=FIND("-",A2)in D2 and=LEN(A2)in E2. Fill both down. The separator position gives four and the total length gives eight on every original row. Use these checks to explain why extracting exactly three and four characters worked. - In a spare F column test
=MID(A2,5,4). It should return exactly the same serial text as RIGHT. Check both formulas against a serial with leading zeros and against 1006, which needs none. - In another spare cell, use
=COUNTIF(B2:B5,"BLK"). It returns three. Compare those rows with their original IDs. This shows the practical benefit of extracting a clean group label rather than manually retyping it for every record.
Convert a value when it really is used as a number
Try =COUNT(C2:C5). It returns zero for this extracted text range. If a different business question needs a numeric reading of the serial suffix, use =VALUE(C2) in G2 and fill down. The numeric results are 42, 7, 129 and 1006. COUNT over G then returns four. This verifies that the values truly changed type. Retain C when its four-character label is part of your reference or presentation.
VALUE interprets text Excel recognizes in your environment. A digit-only string in this exercise has no decimal or group punctuation. For data with regional decimal or grouping separators, verify the format before converting. A function such as NUMBERVALUE can accept explicit separators when the source format is known. Keep the raw label separate so a conversion with the wrong assumptions does not erase meaningful characters.
Prove where fixed width stops working
In a copy of row 2, use BLK-042, with only three serial digits. LEFT still gives BLK, but RIGHT at width four returns -042. LEN gives seven and FIND still reports four. The formula succeeds in returning four characters. The range check is what catches the wrong shape.
Next type BLK-0042 and add one trailing space in a scratch cell. The space becomes the last character, LEN rises to nine, and the four-character right slice contains 042 followed by the space. Inspect the result with a pair of markers you join around it in a separate cell. The test should produce [042 ]. Use the next lesson's cleanup methods only after you know the role of the space in the source.
Lab 2 · Extract an area label between two moving hyphens
A fictional order sheet needs to report the area from a compound store code. Each valid code contains a city prefix, an area and a two-character branch label, separated by exactly two ASCII hyphens. City and area lengths can differ. The space inside College Road is part of its field. Use this fixed delimiter rule to calculate the positions rather than counting characters manually for every row.
Create an Areas worksheet. Put Raw location, First hyphen, Second hyphen, Area length and Area as the headers in A1 through E1. Enter the four original codes below. Keep the Raw location column available throughout, especially when you decide whether an irregular field needs review.
| Raw location | First hyphen | Second hyphen | Area length | Expected area |
|---|---|---|---|---|
| PUN-Kothrud-01 | 4 | 12 | 7 | Kothrud |
| NSK-College Road-01 | 4 | 17 | 12 | College Road |
| NAG-Central-07 | 4 | 12 | 7 | Central |
| SOLAPUR-West-03 | 8 | 13 | 4 | West |
Read the two boundaries into separate helpers
- In B2 enter
=FIND("-",A2). It gives four. Imagine the first separator as the end of the city field. The area starts at the very next position, five. - In C2 enter
=FIND("-",A2,B2+1). It begins looking one character after the first hyphen and finds the second hyphen at twelve. Twelve is its position in the original string. Excel does not reset the numbering when the search starts at five. - In D2 enter
=C2-B2-1. The distance between the two separator positions is eight. Subtract one to avoid counting the separator. Seven characters remain between them. - In E2 enter
=MID(A2,B2+1,D2). This starts at position five and reads the seven area characters. It gives Kothrud, excluding both hyphens. - Fill B2:E2 down through row 5. On the College Road row, expect first at four and second at seventeen. On the Solapur prefix row, expect first at eight and second at thirteen. The new start shifts without changing the extraction rule.
Trace the College Road row one position at a time
Find the city boundary
The first hyphen is at position four. The C in College is the first area character, at position five.
Find the next boundary
Look again from position five onward. The second hyphen is position seventeen, after the full area label including its internal space.
Measure the slice
Seventeen minus four minus one is twelve. MID reads twelve characters starting at five. The last one it reads is character sixteen.
Check the output
The text is College Road. It preserves the internal space and excludes both delimiters and the final branch label.
Test a broken delimiter and a legitimately empty field
A successful extraction is only evidence that the function found enough characters. Before using an extracted area in a report, decide what your source promises. The Areas lab assumed exactly two plain hyphens, a nonempty city, a nonempty area, and a two-character branch label. Feed it a known broken code for each important assumption.
- Missing first separator. Try
NSKCollege Road01. The first FIND returns#VALUE!. Excel cannot calculate the later positions from that value. Inspect the source to learn whether a separator was missing or a different delimiter was promised. - Missing second separator. Try
PUN-Kothrud01. The first position is still four. The second FIND fails. This narrows the problem to the second separator, which is easier to see when helper positions are separate. - Empty middle field. Try
NSK--01. The first separator is at four and the second at five. The computed area length is zero and MID returns empty text. The split operation had enough delimiters. The result still fails the stated nonempty area rule. - An unexpected extra delimiter. Try
NAG-Old-Town-07. The existing calculation returns Old, and the second hyphen is at position eight. If the intended area is Old-Town, the grammar of this code is different. Ask whether the first and last delimiter enclose the area, or whether area labels use an agreed alternate delimiter. - A different dash character. In
PUN–Kothrud–01, the visible separators are en dashes. They are different characters from the ASCII hyphen in your formulas. A FIND that looks for plain hyphens cannot find those en dashes.
Use a check rather than erasing every error
For this contract, the number of ASCII hyphens should be two. The next lesson introduces SUBSTITUTE, which can remove chosen characters. In a helper you can use =LEN(A2)-LEN(SUBSTITUTE(A2,"-","")). The second length comes from a version with the hyphens omitted. The difference counts removed single-character delimiters. Valid rows from Lab 2 give two, and NAG-Old-Town-07 gives three. Also test that the two boundaries leave space for the required area. Delimiter count alone would accept NSK--01.
When your report needs a friendly label for a known problem, use a separate check such as missing hyphen or unexpected number of fields. Keep the raw value available. In a stable import you can choose to show Review on invalid rows, with a helper that distinguishes the invalid shape from an unrelated formula error. Simply returning empty text for every failure makes a missing delimiter look like a deliberately blank field.
Choose the smallest parser that fits the source
Fixed width works when the width is part of the specification
In Lab 1, three letters and four digits are intentional features of the sample code. If the export permits four-letter platform prefixes, a three-character LEFT formula creates an incorrect group label. Suppose the source changes to SHOP-0042. LEFT at width three gives SHO, but the intended prefix is SHOP. A new sample should include this variation before the team approves a formula for the whole batch.
A separator can give you variable widths without a long formula
For a single separator, use =LEFT(A2,FIND("-",A2)-1) to read the left field. It finds the position of the first hyphen and selects the preceding characters. For the right field, use =RIGHT(A2,LEN(A2)-FIND("-",A2)). This chooses the remaining characters after the first hyphen. With SHOP-0042, you get SHOP and 0042. These expressions assume there is one separator, and they preserve any additional characters in the right field if the input unexpectedly contains more.
Check whether you need the first, second or last separator
The Areas exercise explicitly needs the text between the first two separators. For a new code whose area legally contains hyphens, those positions no longer describe the intended end. Use the rules documented by the source. The later delimiter functions lesson shows first and last delimiter searches on versions that support TEXTBEFORE and TEXTAFTER. If you need to keep compatibility with a workbook using traditional functions, retain named helper formulas and test each boundary explicitly.
Once you know how the positional helpers work, this equivalent single-cell calculation can replace them for the valid Areas codes. It calls FIND twice for the first separator and once for the second so that MID has both a start and a length. The separate helpers are usually easier to review at first.
=MID(A2,FIND("-",A2)+1,FIND("-",A2,FIND("-",A2)+1)-FIND("-",A2)-1)
Practice extension · pull a date label and a serial from an ID
Use a separate sheet with a fictional shipment code BLK-261102-004. In this exercise, the middle six characters encode a two-digit year, month and day. The serial is a three-character label. The source owner tells you every year in this sample is in the twenty-first century. These are exercise assumptions, and the prefixes do not describe a real export format.
- Write the character boundaries on paper. The first hyphen is at position four. The second is at eleven. The date label begins at five and runs for six characters. The serial starts at twelve and runs for three.
- Use
=MID(A2,5,6)on the given fixed-width example. The output is the text 261102. Use=RIGHT(A2,3)to get 004. Keep both extracted strings available if the task is to reconstruct the original ID. - If a report needs a calendar date, put the date label in B2. Interpret its year part as 2026 after applying the stated century rule. The month is 11 and the day is 02. A
DATEformula can construct an Excel date after converting the component strings to numbers. - Use
=DATE(2000+VALUE(LEFT(B2,2)),VALUE(MID(B2,3,2)),VALUE(RIGHT(B2,2)))for this verified row. Format the result to show the day, month and year. It should read 2 November 2026. Validate date components against the expected source rules before using the approach on untrusted data. Excel DATE can normalize out-of-range day or month input rather than reporting it as an invalid code. - If a practice task specifically asks for the numeric serial, apply VALUE to 004. It gives four. If you only need the ID suffix for a label, keep the literal zeros. A numeric value does not store the original width.
The lesson on Excel dates develops date values and boundary checks further. Extraction by position is the first step. Interpreting the extracted part as a number, date or identifier is a separate decision based on its meaning.
Check characters that do not look different at first
Two text fields may differ in a way you cannot see immediately. Imported data can include trailing spaces, line breaks, another dash character, or changes to uppercase and lowercase letters. A good diagnostic begins with one sample cell, a way to show boundaries, and the exact expected output. A formula filled down across the entire dataset is easier to trust after that small test passes.
- Record the source width. Compare LEN with the expected width if the field has one. In a variable-width field, lengths still help explain a disagreement between visually similar values.
- Read the exact symbols. Searching for the standard hyphen cannot find a substituted Unicode dash. If the specification allows several symbols, normalize the documented alternatives in a new column before using the extraction rule.
- Test case on purpose. Use FIND for codes where capitalization is significant. Use SEARCH when you are locating a label regardless of capitalization. Verify both against known uppercase and lowercase examples.
- Be aware of Excel text compatibility. Microsoft 365 workbooks can have a compatibility setting that affects how functions such as LEN, MID, FIND and SEARCH count some Unicode characters. In supported version-2 workbooks, certain emoji characters count as one where an older mode counted them as two. A visually grouped symbol can still contain multiple counted parts. These code labs deliberately use ordinary ASCII letters, digits and hyphens for predictable offsets.
Leave the original column in place when cleaning or extracting. A report can use the newly derived prefix or area, and anyone who finds a problem can compare it with the raw code. This also makes it possible to revise the extraction later if the source format changes.
Use search positions for a label review
A fictional marketplace inventory wants to find product names that contain the word Milk, regardless of capitalization. Start with only a few examples. A search can help find a portion of a text value, but it does not know whether the result corresponds to an approved product category. A separate source category would give more reliable grouping when available.
Compare the exact search with a case-insensitive search
- In A2 on a scratch tab, enter
Fresh Milk 500 ml. In B2, use=FIND("milk",A2). The lowercase string is absent, so the result is an error. Try uppercase M, and the match begins at seven. - In C2 use
=SEARCH("milk",A2). It returns seven whether the source uses Milk, MILK or milk. The result is numeric because it represents a character position, not the matching text. - Use
=ISNUMBER(SEARCH("milk",A2))as a separate presence test on a clean text input. It returns TRUE for the example. TryFresh Curd 500 gand it returns FALSE. Keep the search result in another column while diagnosing data so you can see the precise failure. - Test
Milkshake 250 ml. The presence test also returns TRUE because Milk is part of that string. If the intended group is an exact category, check a category field or a whole approved label. Substring matching needs a boundary rule before it becomes classification.
Use a different pair of characters as the boundary
On the same scratch tab, enter Stock [Pune] D4 in A3. Find the left square bracket at position seven and the right square bracket at twelve. Start at eight and take four characters. The text between the brackets is Pune. If you use open and close bracket helpers, begin the second search after the first match so a stray close bracket before the label is not treated as its endpoint.
Use Status? done to see why escaping a wildcard matters. SEARCH with a bare question mark matches a single character, so the first position qualifies. SEARCH with "~?" finds the literal question mark at position seven. FIND with a plain "?" looks for the literal character and also returns seven. Choose the expression based on whether you want a pattern or an exact special character.
Keep the extracted field understandable outside Excel
A worksheet formula can make calculated text look like a manually typed label. Write a short note that names the raw column, states the expected format and identifies the extracted pieces. In the Areas example, the contract says two plain hyphens and a nonempty area between them. For the order serial, the contract says the last four characters make up the label. These notes help someone map an output back to its source when a new batch arrives.
- Store the result in a derived column with a clear name such as Serial text. When another column holds the numeric serial, give the two fields different headings. This prevents a report formula from accidentally losing the expected zeros.
- Test a leading-zero label outside the original worksheet when it will be exported and re-opened. A plain text file can contain the same digits and still be interpreted as a number when another tool imports them. Set the import column type consistently if the label is an identifier.
- Keep a few known pass and fail examples in a training copy. Include one changed prefix length and one missing separator. The next person can verify the formula without inspecting a long list of ordinary rows.
- Check the outcome with a simple reassembly where the original representation should be preserved. For Lab 1, joining the prefix, a hyphen and the stored Serial text should reproduce the original valid ID. Joining the numeric value 42 gives a different written form.
Prove an extraction by assembling the source again
In the Codes practice tab, take a row where the full identifier is split into prefix and serial text. Rebuild it by joining those two columns with a plain hyphen in between. For row 2, use =B2&"-"&C2. The result should match the original BLK-0042, including the zeros. If your worksheet is expected to preserve the original ID precisely, an exact comparison is a useful audit step.
Check the reconstructed text against the source
- Put the reassembled code in H2 and enter
=EXACT(A2,H2)in I2. EXACT compares text with case sensitivity. It returns TRUE for the original valid ID. The two visible pieces are sufficient to rebuild that simple code under the worksheet's delimiter rule. - Temporarily rebuild with the numeric serial from G2 instead. The result becomes BLK-42. The exact comparison changes to FALSE because the written width changed. Reconnect the reconstruction to C2 for the intended preservation check.
- Try a source prefix spelled with lowercase letters. If you apply a function that forces uppercase during extraction, exact reconstruction will also return FALSE. This is appropriate when you are auditing exact retention. If your business rule intentionally standardizes case, document that as a transformation and check it separately.
- Put the missing-digit row from Lab 1 through the same two steps. The initial fixed-width rule gave a serial piece starting with a hyphen. Adding a separator back now creates two hyphens in the reconstructed code, which reveals that the original layout assumption failed.
Remember what successful reconstruction proves
Matching the original confirms that this split preserved the characters needed to rebuild it. It does not establish that the parts are a recognised prefix or an allowed serial. A code starting with an unknown three-letter prefix can still round-trip perfectly. When the report uses a set of permitted codes, check the prefix against the reference list as a second test. When you need a meaningful date, validate the year, month and day independently.
Give the two checks separate roles in the workbook. One tests the extraction procedure. The other tests whether the original record meets the source rules. If either fails, keep its row available for review and use the original text to make the next decision.
If the position itself seems wrong, check the lower boundary. Text positions in these functions start at one. A FIND or SEARCH with a start of zero fails. MID with a start of zero also fails. A zero count in MID is different: it requests no characters at a valid start and gives an empty text result. Inspect which argument is zero before deciding whether your delimiter arithmetic or the source shape needs repair.
Quiz · find the starting position and result
What does RIGHT with four characters return from BLK-0042?
It returns the four-character text 0042. The two leading zeros remain part of that text. Convert it to a number only if you need 42 for a calculation.
At what position is the first hyphen in BLK-0042?
Position four. B, L and K occupy positions one, two and three. Counting begins with one and includes separator characters.
Why is thirteen not the requested length for College Road in NSK-College Road-01?
The separators are at four and seventeen. Their distance is thirteen, which includes one boundary position. Subtract one to get twelve intervening characters, including the internal space.
Which function can find lowercase milk in Fresh Milk 500 ml?
SEARCH can return its starting position seven because it ignores capitalization. FIND with a lowercase search asks for an exact case match and fails on this source.
What does MID return between consecutive hyphens in NSK--01?
The two delimiter positions are adjacent, giving a slice length of zero. It returns empty text. The result should still be reviewed if the source requires a nonempty area label.
Would you use LEFT at width three for a new prefix of SHOP?
The formula returns SHO. If that prefix is allowed to vary in length, find the first separator and take the characters before it. Verify that the separator is present before treating the output as valid.
Interview practice · explain the text contract
What is the difference between LEFT, RIGHT and MID?
LEFT and RIGHT start at an edge and take the requested count. MID needs a position and a length. I would use MID when the piece starts in the middle, such as an area between a city prefix and a branch number.
When should you prefer SEARCH to FIND?
I would choose SEARCH when letter case should not matter or I need a wildcard pattern. FIND gives an exact case-sensitive match without wildcards. I would test both against a known uppercase and lowercase version of the label.
How do you find the second separator using traditional functions?
I would find the first one, then start a second search one character later for a single character delimiter. The later result is still the position in the whole string. I can subtract the two positions and one more to measure the piece between them.
Why can a digit-only result from RIGHT be significant as text?
The result may be a label with a required width such as 0042. The numeric value 42 cannot preserve its original zeros. I would keep the extracted text for identity and convert a separate result only when a calculation needs it.
How would you investigate a code that fails after import?
I would inspect the raw text, its length and the first few delimiter positions. Extra spaces and visually similar dash characters can account for a mismatch. I would preserve the raw code and test the expected valid and invalid inputs before enabling a cleanup formula.
Six cards for a quick extraction check
- LEFT and RIGHT Read a chosen number of characters from the start or the end.
- MID Give a start position and a length. Check the separator positions before counting the characters between them.
- LEN Counts characters, including spaces. Use the result to investigate a code whose width looks wrong.
- FIND Returns an exact case-sensitive match position. It uses the original text positions even when the search starts later.
- SEARCH Returns a case-insensitive match position and supports wildcard patterns. Escape a wildcard character when it is meant literally.
- Text type Keep leading zeros in identifiers. Convert extracted digits separately when the business meaning is numeric.
Next, practise cleaning and replacing characters. If both the extract and the original need to appear in a report, keep them in separate named columns.
Microsoft references
Continue practising: explore the existing Excel Task Hub for additional exercises.