How do I get the =LEFT function in excel, to also take the number zero as the first number?

My data contains a column of room numbers. Each room number's first 2 digits is the floor number where the room is located. The =LEFT function works well for data that does not start with zero. The =LEFT function doesn't work well for data that starts with zero.

How can I get the formula to not ignore the first number when it is zero?

Room Number Formula Floor Number
1214 =LEFT (A2, 2) 12
2354 =LEFT (A2, 2) 23
1876 =LEFT (A2, 2) 18
1567 =LEFT (A2, 2) 15
0403 =LEFT (A2, 2) 40* (should be level 04)
1918 =LEFT (A2, 2) 19
0910 =LEFT (A2, 2) 91* (should be level 09)
2

5 Answers

If all the room numbers are meant to have 4 digits, and the four digits are achieved by formatting numbers less than 1000 with custom format "0000", then you can use

=LEFT(TEXT(A1,"0000"),2)

Edit: FWIW, this approach also works if the value is text.

enter image description here

0

It seems you store room numbers as numbers, this case LEFT is not the best choice, use INT instead:

=INT(A2/100)

To keep the leading zero (04 instead of 4) you need to set number format of the cells to "Custom" with "type": 00

Thanks @fixer1234 for the comment!

enter image description here

5

This sounds like it's a cell formatting issue. Make sure your Room Number column is formatted as Text and not General or Number.

LEFT formula example
This is from LibreOffice, but the behaviour is the same for Excel as long as the cell is formatted as Text.

Based on your comment that you simply custom formatted the value to show a leading 0, you can use this formula to check if there is a leading 0 on a room name and then add one if there isn't:

=IF(LEFT(A1,1)=0,LEFT(A1,2),CONCAT("0",LEFT(A1,1)))

Screenshot to show leading 0 logic formula in action

7

Michael Frank figured out what's going on. A custom format of "0000" adds leading zeros as necessary to display four digits, but it doesn't alter what is in the cell. If you apply that format to 102, the cell will display 0102, but if you use LEN() to test the number of characters, that will show three. You can use this for a solution with a short formula.

enter image description here

In A1, I've got the value 102. In A2 is the same value, custom formatted 0000. The formula in B2 is:

=LEFT(REPT("0",4-LEN(A2))&A2,2)

This subtracts the actual length of the value in A2 from 4, then creates the resulting count of leading zeros and concatenates that to the actual value in A2. Then it takes the left two characters of the result.

Row 3 shows the result when the value in column A is actually four characters.

2

There are already good answers available to your question, but if you want to make this work with the same formula you had used initially:

=LEFT(A1,2)

then just prefix ' (single quote before the number).  See the below screenshot for your reference

e.g., instead of just 0403, make it '0403 and then apply the same formula.

Screenshot for reference below

Add prefix before the number:

Prefixed with '

Works with the same formula:

Works with same formula

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like