Tabellab

LEFT, RIGHT & LEN: Fixed-Width Codes

Lesson 1 of 20Lab

All lessons

You are not signed in — the lab works, but your progress is not saved. Sign up

Lesson 1 of 20 is free. Unlock all 20 hands-on labs, the final exam and your certificate.

Buy course — CHF 9.90
Loading the spreadsheet…

Why text functions save hours every week

Most data does not arrive in the shape you need. An online shop exports one long order ID when you want the country on its own. A bank file puts the account and the reference in one cell. A warehouse system glues a category, a size and a colour into a single product code. If you have ever split hundreds of such codes by hand, or retyped them into separate columns, Excel's text functions are the fix. They cut, measure and rebuild text with formulas, so the result updates when new rows arrive and you never retype a character.

This free lesson is the first of 20 in the course and starts with the three functions everything else builds on: LEFT, RIGHT and LEN. It is written for people who clean exports for a living, even if they would not call it that: e-commerce and operations staff, bookkeepers, analysts, assistants who prepare reports. No prior formula knowledge is needed beyond typing = into a cell. By the end you will split a fixed-width code into its parts, turn text that looks like a number into a real number, and use LEN as a quality check that catches broken IDs before they reach a report.

The examples use Pinewick Home Goods, a fictional online shop for home textiles and kitchenware, the same shop as in the lab above. Its order IDs look like US2026004817: 2 letters for the country, 4 digits for the year and a 6-digit order number. Ten of them sit in A2:A11, and one of them is broken.

The core idea: fixed width means fixed positions

A fixed-width code is one where each part always has the same length and therefore always sits at the same position. In Pinewick's IDs, characters 1 to 2 are always the country, 3 to 6 always the year, and 7 to 12 always the order number. Once you know the positions, you can cut by counting characters, without searching for anything.

  • =LEFT(text, [num_chars]) returns the first num_chars characters. Leave out num_chars and you get 1 character.
  • =RIGHT(text, [num_chars]) returns the last num_chars characters.
  • =MID(text, start_num, num_chars) returns num_chars characters starting at position start_num. You will use it properly in lesson 2, but it is handy for the middle part already.
  • =LEN(text) returns the number of characters, counting letters, digits, spaces and punctuation alike.

One order ID, three fixed parts (LEN = 12)Countrypos 1-2Yearpos 3-6Order numberpos 7-12U1S223042566070849810111712LEFT(A2,2)MID(A2,3,4)RIGHT(A2,6)"US""2026""004817" (text)VALUE(...) = 4817 (number)
The anatomy of a Pinewick order ID. Each part has a fixed position, so LEFT, MID and RIGHT can cut it by counting characters.

One rule surprises almost everybody: text functions always return text. =RIGHT(A2,6) returns "004817", which looks like a number but behaves like a word. You cannot reliably sum it, a comparison such as >4000 treats it as text, and lookups against real numbers fail. You can spot it because Excel aligns text to the left of the cell while numbers sit on the right. To get the number 4817, wrap the result in VALUE: =VALUE(RIGHT(A2,6)). A shorter alternative you will see in many workbooks is the double minus, =--RIGHT(A2,6), which converts by negating twice. Both drop the leading zeros, because the number 4817 has no leading zeros; if you need them for display, that is a job for the TEXT function in lesson 6.

Worked example: splitting Pinewick's order IDs

Here is the export with the results of the lab. Column B holds the country, C the order number as a real number and D the length of each ID. The formulas are written for row 2 and copied down to row 11.

RowOrder ID (A)Country (B)
=LEFT(A2,2)
Order no. (C)
=VALUE(RIGHT(A2,6))
Length (D)
=LEN(A2)
2US2026004817US481712
3UK2026004818UK481812
4DE2026004822DE482212
5US2026004830US483012
6CA2026004833CA483312
7UK2026004841UK484112
8US2026004852US485212
9DE202604866DE60486611
10AU2026004860AU486012
11US2026004871US487112
IDs with wrong length (D13): =COUNTIF(D2:D11,"<>12")1

Look at row 9. The ID DE202604866 has only 11 characters: one digit was lost somewhere, probably when the ID was typed or exported. LEFT does not notice, because the first two characters are still the country. RIGHT does notice, but silently: it still takes the last 6 characters, and because the code is one character short, those 6 characters now start inside the year. The result is 604866, an order number that does not exist. Nothing in the cell looks wrong. Only LEN tells you that the input itself is broken.

LEN of each order ID (valid = 12)dashed line = 1212A212A312A412A512A612A712A811A912A1012A11Peach = wrong length: A9 (DE202604866) lost a digit, so RIGHT(A9,6) gives 604866.
LEN as a quality check: nine IDs have the expected 12 characters, one has 11. Any value other than 12 means that every part cut by position from that ID is suspect.

The year can be cut in two ways. With MID: =MID(A2,3,4) starts at character 3 and takes 4 characters. With what you already know: =RIGHT(LEFT(A2,6),4) first takes the first six characters, US2026, and then the last four of those, 2026. Nesting LEFT and RIGHT like this is a useful trick when you can remember only two functions, but MID is easier to read.

Once the country sits in its own column, ordinary counting works. =COUNTIF($B$2:$B$11,"US") returns 4, and a small summary by country turns into a chart in seconds. This is the real payoff of splitting codes: the parts become fields you can filter, count, sum and chart.

Orders per country, counted from =LEFT(A2,2)01234USUKDECAAU42211
Orders per country for the ten IDs, counted with COUNTIF on the column extracted by LEFT (example data from the lab).

Step by step: cutting codes in Excel

  1. Click B2 and type =LEFT(A2,2), then press Enter. The cell shows US.
  2. Copy it down: double-click the fill handle at the bottom-right corner of B2, or select B2:B11 and press Ctrl+D.
  3. Click C2 and type =VALUE(RIGHT(A2,6)). The result 4817 is right-aligned, which confirms it is a number. Copy it down.
  4. Click D2, type =LEN(A2) and copy it down. Scan the column for anything that is not 12.
  5. In D13, count the bad IDs with =COUNTIF(D2:D11,"<>12"). If you prefer a single formula that does not need the helper column, use =SUMPRODUCT(--(LEN(A2:A11)<>12)).
  6. Make the problem visible: select A2:A11, choose Home > Conditional Formatting > New Rule > Use a formula, enter =LEN($A2)<>12 and pick a fill colour. Broken IDs now light up as soon as they arrive.
  7. If you only need a one-off split and no formulas, Data > Text to Columns > Fixed width lets you click the break lines between positions 2 and 6. It is fast, but it does not update when the data changes, and it would cut the broken ID in the same wrong place.
Flash Fill (Ctrl+E, or Data > Flash Fill) can guess a split from one or two examples you type. It is great for quick jobs, but like Text to Columns it produces static values, and it can guess a pattern you did not intend. For anything that repeats every week, formulas are the safer choice.

Common mistakes and how to spot them

  • Forgetting that the result is text. Symptom: a SUM of extracted numbers returns 0, or a lookup against a number list returns #N/A. Check: the values are left-aligned, or =ISTEXT(C2) returns TRUE. Fix: wrap in VALUE or use --.
  • Cutting from the wrong end. =RIGHT(A4,2) returns the last two digits of the order number, not the country. Ask yourself where the part sits: at the start (LEFT), at the end (RIGHT) or in the middle (MID).
  • Taking too many characters. =RIGHT(A6,10) includes the year, so the order number becomes 2026004833. Count the part on paper first, or check the result with LEN.
  • Trusting fixed positions without checking the length. One missing or extra character shifts every part. Always add a LEN column (or a conditional format) next to position-based splits.
  • Invisible spaces. An ID copied from a web page or a PDF can carry a trailing space, so LEN shows 13. =TRIM(A2) removes extra regular spaces. A non-breaking space, character 160, is not removed by TRIM; replace it with =SUBSTITUTE(A2,CHAR(160),""), which you will meet in lesson 3.
  • Applying LEFT to dates or formatted numbers. Text functions work on the stored value, not the display. A date shown as 03/15/2026 is stored as a serial number, so =LEFT(A2,2) returns the first two digits of that serial, not the month. Use MONTH or TEXT for dates.
  • Counting the wrong thing. =COUNTIF(D2:D11,12) counts the valid IDs. For the broken ones you need "<>12", which means "not equal to 12".

Making the split safe: let the formula refuse broken IDs

A LEN column tells you that something is wrong, but the order number in C9 still shows 604866, and anyone who copies column C into another report copies the wrong number with it. A better habit is to let the extraction formula itself refuse to work on a broken input. Combine the cut with an IF test on the length:

=IF(LEN(A2)=12,VALUE(RIGHT(A2,6)),"Check ID")

For the nine valid IDs the result is the same as before. For row 9 the cell now says Check ID instead of inventing an order number. That message travels with the data: a SUM over column C simply skips the text, a filter on Check ID lists every problem row, and nobody has to remember to look at a separate helper column. The same pattern works for the country, =IF(LEN(A2)=12,LEFT(A2,2),"Check ID"), although the country is less at risk because it sits at the start of the code and LEFT is not affected by a missing digit further right.

You can make the check stricter as your data demands. =AND(LEN(A2)=12,ISNUMBER(--MID(A2,3,10))) returns TRUE only when the ID has 12 characters and characters 3 to 12 are all digits, so it also rejects an ID like US2026OO4817 where two zeros were typed as the letter O. You can add a test for the year with MID(A2,3,4)="2026" if every ID in the export should come from the same year. Each extra test is cheap, and together they turn a list you hope is clean into a list you know is clean.

LEFT or RIGHT? A quick way to decide

When you meet a new code, write one example on paper and number the characters, exactly like the diagram above. Then ask two questions for every part you need. Is its position fixed counted from the start? Then LEFT (for the first part) or MID (for any later part) is the tool. Is its length fixed counted from the end, even when the start varies? Then RIGHT is the tool. A common real case is a code whose prefix has a variable length but whose last six characters are always the number, for example OUT-US-004817 and US-004817: RIGHT with 6 works for both, while LEFT and MID would need different positions for each.

If neither the start nor the end gives you a fixed position, because the parts are separated by hyphens, spaces or slashes of varying distance, counting is no longer enough. You then need to find the delimiter first and cut relative to it, which is exactly what lesson 2 of this course teaches with FIND, SEARCH and MID. Knowing when fixed positions stop working is just as important as knowing how to use them.

LEFT, RIGHT, MID and LEN work in every version of Excel, in Excel for the web and on the Mac, and they behave the same in Google Sheets. Newer Microsoft 365 builds also offer TEXTBEFORE, TEXTAFTER and TEXTSPLIT for delimiter-based splitting, but for fixed-width codes the classic functions remain the clearest choice.

Excel Text Functions course cover: LEFT, MID, FIND and SUBSTITUTE used to clean product and order codes
Excel Text Functions: 20 labs at one fictional online shop, from fixed-width codes to parsed product codes and a category chart.

Quick reference: functions and shortcuts from this lesson

Function or keyWhat it doesExample with US2026004817
LEFTFirst n characters=LEFT(A2,2) returns US
RIGHTLast n characters=RIGHT(A2,6) returns "004817"
MIDn characters from a start position=MID(A2,3,4) returns "2026"
LENNumber of characters, spaces included=LEN(A2) returns 12
VALUE / --Turns number-like text into a number=VALUE(RIGHT(A2,6)) returns 4817
COUNTIFCounts cells that meet a condition=COUNTIF(D2:D11,"<>12")
SUMPRODUCTCounts or sums an array test in one formula=SUMPRODUCT(--(LEN(A2:A11)<>12))
TRIMRemoves leading, trailing and double spaces=LEN(TRIM(A2))
Ctrl+DFill the formula down a selectionSelect B2:B11, Ctrl+D
Ctrl+EFlash Fill (static values)One-off splits

Real-world uses

  • E-commerce order exports: split marketplace order IDs into country and number, as in the lab, so you can count orders per market and match them against payment reports.
  • Bank and payment references: many references have a fixed layout, for example a branch code followed by an account part. LEFT and RIGHT pull the parts apart, and LEN rejects references with a missing digit before they are booked.
  • Product and part numbers: a code such as a 3-letter category prefix plus a 5-digit number becomes a category column with =LEFT(A2,3), ready for a PivotTable.
  • Postal codes and phone numbers: take the first digits of a postal code for a regional summary, or the last four digits of a phone number for a masked display such as ="***"&RIGHT(A2,4).
  • Data-quality checks before an import: a LEN column and one COUNTIF tell you in a second whether a list of IDs is clean enough to load into another system.

What you will build in the rest of this course

Real data is rarely this tidy, and the seven paid lessons stay with Pinewick Home Goods to handle everything that fixed positions cannot. Each one is a hands-on lab with an instant check on every cell.

FIND vs SEARCH and MID: Extract Text Between Delimiters. You pull the colour and the size out of marketplace product titles where the parts have different lengths, flag the linen products and learn why FIND and SEARCH sometimes disagree.

SUBSTITUTE & REPLACE: Swap Text and Target the nth Occurrence. You migrate SKUs to a new separator, count their parts, cut everything after the last hyphen and rebrand a prefix by position, the kind of bulk change that is painful by hand.

TRIM, CLEAN, PROPER, UPPER & LOWER: Tidy Customer Data. You clean newsletter sign-ups into proper names, lower-case emails and promo codes without spaces, and count which names were already clean with a case-sensitive test.

Joining Text: &, CONCAT and TEXTJOIN. You build display names, one-line shipping addresses that skip empty fields, customer codes and a list of every customer in a city, ready for shipping labels.

TEXT and Number Formats: Labels, IDs and Leading Zeros. You pad invoice numbers, restore the lost leading zeros of ZIP codes and build readable labels that combine dates and dollar amounts.

Splitting Full Names, Emails and Addresses. You split a customer list into first name, last name (middle names included), email domain and state, with formulas that keep working when the list grows.

Mid-course Project: Parse Product Codes and Chart Counts by Category. You parse categories out of mixed shop and outlet product codes, count lines and units per category and chart the result, using everything from the earlier labs.

Summary

LEFT takes characters from the start, RIGHT from the end and LEN counts them all. For fixed-width codes that is enough to split every part, as long as you remember two rules: the results are text until you convert them with VALUE or --, and position-based cuts are only as reliable as the length of the input, so always check it with LEN. Now scroll up to the lab, split Pinewick's ten order IDs, find the broken one and let the Coach check your formulas. It takes about twelve minutes.

Frequently asked questions

How do I extract the first few characters of a cell in Excel?

Use LEFT with the number of characters you need, for example =LEFT(A2,2) for the first two. If you leave out the number, LEFT returns only the first character.

Why does RIGHT return a number I cannot add up?

Text functions always return text, even when it looks like a number. Convert it with =VALUE(RIGHT(A2,6)) or =--RIGHT(A2,6) and it becomes a real number you can sum and compare.

Does LEN count spaces?

Yes. LEN counts every character, including leading, trailing and double spaces, which makes it a good way to find hidden spaces. =LEN(TRIM(A2)) shows the length without the extra regular spaces.

How do I get characters from the middle of a text in Excel?

Use MID with a start position and a length, for example =MID(A2,3,4) returns four characters starting at position 3. Nesting RIGHT(LEFT(A2,6),4) gives the same result.

How can I count cells whose text is not a certain length?

Put =LEN(A2) in a helper column and count with =COUNTIF(D2:D11,"<>12"), or do it in one step with =SUMPRODUCT(--(LEN(A2:A11)<>12)).

Should I use formulas, Flash Fill or Text to Columns to split codes?

Flash Fill and Text to Columns are quick for one-off jobs but produce static values. Formulas with LEFT, RIGHT and MID update automatically when the data changes, so they are better for recurring exports.

Keep going: Excel Text Functions: LEFT, MID, FIND, SUBSTITUTE & Clean Data

This free lab is lesson 1 of 20. Unlock the other 19 hands-on labs, the final exam and your certificate for a one-time CHF 9.90.

  1. FIND vs SEARCH and MID: Extract Text Between Delimiters
  2. SUBSTITUTE & REPLACE: Swap Text and Target the nth Occurrence
  3. TRIM, CLEAN, PROPER, UPPER & LOWER: Tidy Customer Data
  4. Joining Text: &, CONCAT and TEXTJOIN
  5. TEXT and Number Formats: Labels, IDs and Leading Zeros
  6. Splitting Full Names, Emails and Addresses
  7. Mid-course Project: Parse Product Codes and Chart Counts by Category
  8. TEXTBEFORE & TEXTAFTER: Delimiters Without Position Maths
  9. The nth Item of a List: TEXTSPLIT, INDEX and the SUBSTITUTE-REPT Trick
  10. Marketplace Title Limits: Word Counts, Character Caps and Cutting at a Whole Word
  11. Hidden Characters: CHAR, CODE, CLEAN and Line Breaks in Address Cells
  12. Pattern Checks with REGEXEXTRACT, REGEXTEST and REGEXREPLACE
  13. Check Digits: Catching Typos in Item Numbers with MID and SUMPRODUCT
  14. Privacy-Safe Exports: Masking Phone Numbers and Emails with REPT
  15. Reading Landing-Page URLs: UTM Source and Campaign, Counted and Charted
  16. Sort Keys for Mixed Codes: Why A10 Sorts Before A2
  17. Classifying Search Queries with a Keyword Table
  18. Self-Writing Report Lines: Plurals, Changes and Lists in One Sentence
  19. Final Project: Marketplace Feed Clean-Up Dashboard

Buy course — CHF 9.90 See the full course