• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

How to find value in text

I am not sure which function to use to find the text I am looking for.  I thought it was the CHARINDEX but it is not returning the desired results.  I am looking for the name which starts in the same location every time but can be different lengths.  The end of the name is designated by a space.  I am using the following logic:

SUBSTRING(MASTER24_HISTORY.MRHISTORY,21,CHARINDEX(' ', LEFT(MASTER24_HISTORY.mrHISTORY,22)))

However it is pulling more than just the name.  Any idea why?
0
Gary Harper
Asked:
Gary Harper
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi GHarper,

It looks like you're mixing techniques.  :)

First limit the variable (chop off the bit to the right:
  LEFT (item, charindex (item, ' ', start))

Then grab the desired portion:
  SUBSTRING (LEFT (item, charindex (item, ' ', start), start, limit)

Limit is the size of the variable.  You can compute a value, but this is easier to code.


Good Luck,
Kent
0
 
lcohanDatabase AnalystCommented:
I suggest you look for PATINDEX ( '%pattern%' , expression ) to find the possition of the ' ' - or space char in your string and build/calculate your SUBSTRING based on that.
0
 
Gary HarperAuthor Commented:
kdo pointed me in the right direction.  I ended up using

SUBSTRING(item,21,CHARINDEX(' ',item,21) - 21) as 'Username'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now