Parsing value from string to compare with in sql statement


I want to be able to parse an 'id value' from a description field.  

So values for this id are in bold and underlined for you.  They will always be after the : and before the E of Emplid.

example 1.  REQUEST_ID:RV3-5 EMPLID:2005078 RDC:RA,0,A
example 2.  REQUEST_ID:13 EMPLID:1001716 RDC:RA,0,A

Is there a way you can do this in sql?

Right now the creator of the query is doing this:


This will not work for the RV3-5 ...

Any help would be greatly appreciated.

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.

PortletPaulEE Topic AdvisorCommented:
I a assuming that "REQUEST_ID:" and " EMPLID:" always exists in the string.
with sample as (
    select 'REQUEST_ID:RV3-5 EMPLID:2005078 RDC:RA,0,A' as description
    union all
    select 'REQUEST_ID:13 EMPLID:1001716 RDC:RA,0,A'

*, left(replace(description,'REQUEST_ID:',''), ca.pos) as id
from sample
cross apply (
    charindex(' EMPLID:', replace(description,'REQUEST_ID:','')) pos
    ) ca

Open in new window

nb you don't have to use the cross apply it could be done like this:
*, left(replace(description,'REQUEST_ID:',''), charindex(' EMPLID:', replace(description,'REQUEST_ID:',''))) as id
from sample

Open in new window

You could use


Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Just to share a slightly different approach, this assumes:
  • REQUEST_ID: is in the text
  • REQUEST_ID: can be preceded by other text
  • The ID to be extracted is followed by a space
  • EMPLID: doesn't have to be there

select substring(description, charindex('REQUEST_ID:', description)+11, charindex(' ', substring(description, charindex(description, 'REQUEST_ID:')+11, 999))-2) as id

Open in new window


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
5 Ways Acronis Skyrockets Your Data Protection

Risks to data security are risks to business continuity. Businesses need to know what these risks look like – and where they can turn for help.
Check our newest E-Book and learn how you can differentiate your data protection business with advanced cloud solutions Acronis delivers

BrockAuthor Commented:
Hi Bill,

I am implementing the solution you have provided -  

I  am trying to also work on the emplid portion  which will also move when the request id expands --

What is the significance of the '-2' ?    I am assuming the 999 - is an approximate max on the string length.  

I would like to reference the numbers as lengths of field values if possible.

Thanks, Brock
BrockAuthor Commented:
Sorry,  I just saw the substring on the outside.  Too tired  :-)
BrockAuthor Commented:
Okay - I broke down the individual parts and all is clear.

Thank you to all of you for your assistance.

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

From novice to tech pro — start learning today.