RichardAtk
asked on
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
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
Msgbox Split(<your value>,"-")(2)
Using a split function makes this fairly routine.
Here is the split function I used:
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
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