Based on your description, you want to be using the MID()
function, not LEFT()
, nor RIGHT()
.
Documentation is your friend. If you were to look through the documentation, this is the page where you would find information about mid.
From that page:
MID
Description: Returns the specified number of characters from the middle of a text string given the starting position.
Use: MID(text, start_num, num_chars) and replace text with the field or expression to use when returning characters; replace start_num with the number of characters from the left to use as a starting position; replace num_chars with the total number of characters to return.
Example: MID(Division, 3, 4) returns four characters of the Division name beginning with the third character from the left. On a user record, this represents the department code.
From that, we see that the function doesn't take the first and last indices of the characters you want to extract, but rather the first index, and the total number of characters to extract.
Thus, extracting characters 6-8 in a string would look like this:
MID(MyStringField__c, 6, 3)
We start at index 6 (this function uses 1-indexed positions, so the 6th character is index 6), and want the 6th, 7th, and 8th characters (a total of 3).
+edit:
As a bonus, the bit in the documentation about MID()
returning the characters from the middle of the string is a bit misleading. There's nothing stopping you from using it to return characters from any location within the string.
You could even use MID()
to return the same characters as LEFT()
or RIGHT()
do (however, using MID()
like this would be prone to issues, and it would be safer and easier to just use LEFT()
or RIGHT()
).
//Equivalent to LEFT(string, number_of_chars)
MID(string, 1, number_of_chars)
// Equivalent to RIGHT(string, number_of_chars)
MID(string, LENTH(string) - number_of_chars + 1, number_of_chars)
MID()
instead ofRIGHT()
.