Menu

LEFT, RIGHT and MID in Excel: Extract Part of a Text

=LEFT(A2,3) returns the first 3 characters of A2, =RIGHT(A2,2) the last 2, and =MID(A2,5,4) 4 characters starting at the 5th. Combine them with FIND and LEN when the length varies.

Every sheet on this page is live: change a number or a formula and it recalculates.

=LEFT(A2,3) returns the first 3 characters of the text in A2, =RIGHT(A2,1) the last character, and =MID(A2,5,4) returns 4 characters starting at the 5th. For the code NYC-2041-B that is NYC, B and 2041.

Take a code apart
B2
ABCD
1CodeCityNumberSize
2NYC-2041-BNYC2041B
3LAX-1187-ALAX1187A
4SFO-3302-CSFO3302C
5BOS-0915-BBOS0915B
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Click C2 and change the 5 to 4: the result now starts at the dash. Spaces and punctuation count as characters, and the first character is position 1.

Syntax

=LEFT(text, [num_chars])
=RIGHT(text, [num_chars])
=MID(text, start_num, num_chars)
  • num_chars is optional in LEFT and RIGHT and defaults to 1.
  • If you ask for more characters than there are, you get the whole text, not an error. =LEFT("Ann",10) is Ann.
  • A negative count gives #VALUE!, for example =LEFT(A2,LEN(A2)-3) on a text shorter than 3 characters.

When the length varies: LEFT, RIGHT and MID with FIND

Codes are rarely all the same length. Use FIND to locate the separator and let the formula work out the count:

Text before and after the dash
B2
ABC
1CodeBefore the dashAfter the dash
2NY-2041NY2041
3BOSTON-15BOSTON15
4LA-30087LA30087
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.
  • FIND("-",A2) returns the position of the dash, 3 in NY-2041.
  • LEFT(A2,3-1) takes the 2 characters before it.
  • MID(A2,3+1,50) starts after the dash and takes up to 50 characters, more than any code has, so it reaches the end.

In Microsoft 365 and Excel 2024, TEXTBEFORE(A2,"-") and TEXTAFTER(A2,"-") give the same results in a shorter formula; see splitting text.

Remove the first or last characters

To drop characters, keep the rest. LEN gives the total length, and you subtract what you want to remove:

Remove characters from the start or end
B2
ABC
1TextWithout first 3Without last 2
2ID-5821058210ID-582
3ID-77347734ID-77
4ID-120120ID-1
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.
Last four digits
B2
AB
1CardLast four
24111 2222 3333 9876
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Your turn: In B2, return the last 4 characters of the card number in A2.

Extract a number from a code

LEFT, RIGHT and MID always return text, even when the characters are digits. Text digits look like numbers but SUM skips them, which is why a total of extracted numbers can come out as 0:

Text digits vs numbers
B2
ABC
1OrderMID (text)VALUE(MID)
2SO-0120-X0120120
3SO-0345-Y0345345
4SO-1000-Z10001000
5Total01465
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Column B shows 0120 with its leading zero because it is still text, and B5 totals 0. Column C converts each piece with VALUE and the total is right. =--MID(A2,4,4) and =MID(A2,4,4)*1 convert the same way; see text to number for more.

Pull out the quantity
B2
AB
1ItemQuantity
2PK-045-BAG
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Your turn: The quantity sits between the two dashes and always has 3 digits. In B2, extract it as a number you can add up.

Common mistake: LEFT on a date

A date is stored as a number, so =LEFT(B2,4) on the date 2026-03-15 does not return 2026: it returns 4609, the first 4 digits of 46096, the serial number behind the date.

LEFT reads the number under the date
C2
ABCD
1EventDateLEFT(date,4)Year
2Launch2026-03-1546092026
32026
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Use YEAR(B2) for the year as a number, or TEXT(B2,"yyyy") for the year as text. MONTH, DAY and TEXT with "mmm" or "dd" cover the other parts.

Frequently Asked Questions

How do I extract the first N characters in Excel?

Use LEFT with the count: =LEFT(A2,3) returns the first 3 characters. Without the second argument, =LEFT(A2) returns only the first character.

How do I remove the first or last characters in Excel?

Keep the rest with LEN: =RIGHT(A2,LEN(A2)-3) removes the first 3 characters and =LEFT(A2,LEN(A2)-2) removes the last 2.

Why can't I add up the numbers MID returns?

LEFT, RIGHT and MID always return text, and SUM skips text. Turn the result into a number with VALUE or two minus signs: =VALUE(MID(A2,5,4)) or =--MID(A2,5,4).

How do I get the text before a character in Excel?

Find the character's position and take one less: =LEFT(A2,FIND("-",A2)-1) returns everything before the first dash. In Microsoft 365, =TEXTBEFORE(A2,"-") does the same.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED