Extract from Excel two character after the second - in a cell.

Hi

I am wanting to extract the colour code into a separate column from some product codes.  The codes have slightly different lengths what I need to do is look at the code and after the second - pick the two next character from the product code.
Example spreadsheet of what I am after is attached or
BBB-G19-BE-L            (returns BE)
DR-17-OL                    (returns OL)
ZZG-27-CH-S              (returns CH)
Many thanks
Exampleproductcode.xls
RichardAtkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Msgbox Split(<your value>,"-")(2)
Brian CroweDatabase AdministratorCommented:
Using a split function makes this fairly routine.

DECLARE @Product TABLE
(
	ProductCode		VARCHAR(100)
);

INSERT INTO @Product
VALUES ('BBB-G19-BE-L'),
	('CCC-T01-BE'),
	('DR-17-OL'),
	('CR-47-OR-8'),
	('CRR-48-OR-4'),
	('CR-51-OR-2'),
	('XXA-25-BL'),
	('ZAA-26-BL'),
	('ZZG-27-CH-S'),
	('ZZG-31-EB-L');

SELECT ProductCode, S.Value AS ColourCode 
FROM @Product
OUTER APPLY dbo.SplitText(ProductCode, '-') AS S
WHERE S.[Index] = 3

Open in new window


Here is the split function I used:

ALTER FUNCTION [dbo].[SplitText]
(
	@Text		VARCHAR(MAX),
	@Delimiter	VARCHAR(10) = ','
)    
RETURNS @Item TABLE
(
	[Index]		INT IDENTITY(1,1) NOT NULL,
	Position	INT,       
	Value		VARCHAR(MAX)   
) 
AS 
BEGIN    
	SET @Text = RTRIM(@Text) 
	   
	DECLARE @Position INT, @i INT, @s VARCHAR(MAX)    
	SET @Position = 1     
	
	WHILE @Position < LEN(@Text)       
		AND CHARINDEX(SUBSTRING(@Text, @Position, 1), @Delimiter) > 0       
		SET @Position = @Position + 1   
		
	WHILE @Position <= LEN(@Text)    
	BEGIN       
		SET @i = PATINDEX('%[' + @Delimiter + ']%',          
			SUBSTRING(@Text, @Position, len(@Text) - @Position + 1))       
		IF @i > 0       
		BEGIN          
			SET @i = @i + @Position - 1          
			IF @i > @Position          
			BEGIN             
				-- @i now holds the earliest delimiter in the string            
				SET @s = SUBSTRING(@Text, @Position, @i - @Position)            
				INSERT INTO @Item             
				VALUES (@Position, @s)         
			END          
			SET @Position = @i + 1           
			WHILE @Position < LEN(@Text)             
				AND CHARINDEX(SUBSTRING(@Text, @Position, 1), @Delimiter) > 0             
				SET @Position = @Position + 1       
		END      
		ELSE       
		BEGIN         
			SET @s = SUBSTRING(@Text, @Position, LEN(@Text) - @Position + 1)         
			INSERT INTO @Item          
			VALUES (@Position, @s) SET @Position = LEN(@Text) + 1      
				
		END    
	END   

	RETURN 
END

Open in new window

Martin LissOlder than dirtCommented:
I missed the fact that you want a formula. Put this in A2 and copy down.

=TRIM(MID(SUBSTITUTE(A2,"-",REPT(" ",10)),COLUMN(C2)*10-9,10))

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RichardAtkAuthor Commented:
Perfect that's the one thanks I had to use a formula due to the way the spreadsheet worked within a reporting program - this is great many thanks.
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you
including these two new ones.
Creating your own Excel Formulas and doing the impossible
A Guide to Writing Understandable and Maintainable VBA Code
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.