return specific values from a field in sql 2008

I have a column containing a string

"Field [Status] was changed from [Operational] to [Faulty] Field [Status] was changed from [Faulty] to [Decomissioned]"

What I am after is a t-sql script to show each of the values in a [Bracket] in separate columns

Any ideas

Regards
DarrenJacksonAsked:
Who is Participating?
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.

Brian CroweDatabase AdministratorCommented:
Here is a function to extract the Nth bracketed string...

CREATE FUNCTION dbo.GetNthBracket
(
	@Text			NVARCHAR(MAX),
	@Index			INT
)
RETURNS NVARCHAR(100)
AS

BEGIN
	DECLARE @Value	NVARCHAR(100),
		@Start	INT,
		@End		INT,
		@i			INT = 1
		
	SELECT @Value = '', @Start = CHARINDEX('[', @Text)
	IF @Start = 0
	BEGIN
		RETURN NULL
	END

	WHILE @i < @Index
	BEGIN
		SELECT @Start = CHARINDEX('[', @Text, @Start + 1),
			@i = @i + 1
	END
	
	IF @Start > 0
	BEGIN
		SET @End = CHARINDEX(']', @Text, @Start)
		SET @Value = SUBSTRING(@Text, @Start + 1, @End - @Start - 1)
	END
	ELSE
	BEGIN
		RETURN NULL
	END
	
	RETURN LTRIM(RTRIM(@Value))
END
GO

Open in new window


Use it like...

SELECT dbo.GetNthBracket(@Column, 1) AS B1,
   dbo.GetNthBracket(@Column, 2) AS B2,
   dbo.GetNthBracket(@Column, 3) AS B3,
   ...
FROM myTable
0

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
DarrenJacksonAuthor Commented:
excellent just the ticket

Cheers
0
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 SQL Server

From novice to tech pro — start learning today.