Team - I have a field in a table where the data is in below format.

MATA YES [2000534870]
MATA YES [2000534870] NO
AAIG NOW [2000555565] ALTREK

I need to extract only the number in between the brackets. What query would get me this result? pls help
ManjuIT - Project ManagerAsked:
Who is Participating?

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

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.

Leo TorresSQL DeveloperCommented:
DECLARE @str varchar(100)

SET @str = 'MATA YES [2000534870]'

SELECT Replace(SUBSTRING(@str,charindex('[',@str,0)+1,Len(@str)),']','')

Open in new window

You must come from a Excel background sql does not have a MID function for strings.
Leo TorresSQL DeveloperCommented:
if you dont want a replace you can do this one

SELECT SUBSTRING(@str,charindex('[',@str,0)+1, (charindex(']',@str,0)-charindex('[',@str,0)-1 ))

Open in new window

This one is less efficient since it has to parse twice.

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
Saurabh Singh TeotiaCommented:
Try this...

Replace @field with your actual field name...

select SUBSTRING(@field,charindex("[",@field)+1,CHARINDEX("]",@field)-CHARINDEX("[",@field)-1)
from your table

Open in new window

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 2008

From novice to tech pro — start learning today.