Expression to get the first 5 characters from a field

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
I have two fields [LastName] and [FirstName]

I need a calculated field where the expression where if the length of the last name is less than 6 characters the expression uses all the characters but if its not it uses the first five (left(Lastname,5)) And then on the First name if its less than 3 characters it uses all the characters or it uses the first two --- then it concatenates the two.

If this was excel it would be something like this;

Concatenate(If(Len[LastName]<6,Left([LastName],5),[LastName)),(If(Len[FirstName]<3,Left([FirstName],2),[LastName))
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Excel and access are similar on these things, with just a few minor differences:
- Instead of IF, Access uses IIF. The structures are exactly the same.
- Instead of Concatenate, just use & between the things you want to combine together (Excel has the & functionality too).

Then LEN and LEFT functions work the same in both.

However, when do you even need to check for the length at all? If it is less than 6 characters, taking the left-most 5 will already pull all those characters, i.e.
Code:
[COLOR=#333333]Left([LastName],5) & [/COLOR][COLOR=#333333]Left([FirstName],2)[/COLOR]
 
Upvote 0
Thanks, Joe. I guess I was too wrapped up in the bigger picture to think that through. Youre right, I don't need to worry about the lengths. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,221,699
Messages
6,161,367
Members
451,700
Latest member
Eccymarge

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top