TSQL pull out part of string

How can I get the procedure name only out of my text field? I have a table that list SQL text and I want to pull the procedure name out so basically I need everything after the word 'procedure' ending at the ] symbol. So in my example below I want to pull out the string [dbo].[FileDOCWORKSTranscription]


TEXT FIELD
 CREATE       procedure [dbo].[FileDOCWORKSTranscription]  /* 1  */   @InterfaceSourceID       int  /* 2  */ , @EXTPatientID            varchar (20)  /* 3  */ , @RegSystem               varchar (80)  /* 4  */ , @PatientMRN              varchar (255)  /* 5  */ , @PatientOrg              uniqueid  /* 6  */ , @PatientLastName         varchar (255)  /* 7  */ , @PatientFirstName        varchar (255)  /* 8  */ , @PatientDOB              datetime  /* 9  */ , and encounter.Provider2ID = @AuthorID    and DTTM = @EncounterDTTM    an blah blah blah and so on and so on for many more rows
LVL 1
cheryl9063Asked:
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.

CIC AdminCommented:
Not pretty, but assuming all records will have two sets of brackets, you can try something like :

SELECT
  substring(textfield, charindex('[', textfield), charindex(']', textfield, charindex(']',textfield)+1  ) - charindex('[', textfield)+1)
FROM
  yourtable


Basically, it is just calculating the position of the first open bracket :

     charindex('[', textfield)

and length between the first open bracket and the second closing bracket :

     charindex(']', textfield, charindex(']',textfield)+1  ) - charindex('[', textfield) +1

Once you have those two numbers you can put them into the SUBSTRING function :

     SUBSTRING ( expression, start, length )

If your procedures are displayed with different amounts of brackets, the formula will have to be modified slightly.

Good luck,
Kent
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
CIC AdminCommented:
Did you get a chance to try out my solution?
0
cheryl9063Author Commented:
Sorry I forgot about this! Thanks!
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
Query Syntax

From novice to tech pro — start learning today.