Extract data that is between special characters in a field

How would I read tablea (flda)
and only extract what is between | in below records:

HDisbursements_5 |ACCTAP| _1
EDI Support Team_4 |ACCTEDI| _2
Master Data Team_3 |ACCTDATA| _3
Payment Management_2 |ACCTCASH| _4
Please Select Your Group_1 |-->| _5

I only want to extract the following from above records :

ACCTAP
ACCTEDI
ACCTDATA
ACCTCASH
-->
thayduckProgrammer AnalystAsked:
Who is Participating?

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

x
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.

plusone3055Commented:
select SUBSTRING(flda,CHARINDEX('|',flda)+1,CHARINDEX('|',flda)-CHARINDEX('|',flda)-1)

Open in new window

thayduckProgrammer AnalystAuthor Commented:
select SUBSTRING(flda,CHARINDEX('|',flda)+1,CHARINDEX('|',flda)-CHARINDEX('|',flda)-1)


 [flda] VARCHAR(50)

Msg 537, Level 16, State 2, Procedure SC_procRptLstOwner, Line 101
Invalid length parameter passed to the LEFT or SUBSTRING function.
plusone3055Commented:
select SUBSTRING(flda,CHARINDEX('|',flda),CHARINDEX('|',flda)-CHARINDEX('|',flda)-1)
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

thayduckProgrammer AnalystAuthor Commented:
Same error

Msg 537, Level 16, State 2, Procedure SC_procRptLstOwner, Line 105
Invalid length parameter passed to the LEFT or SUBSTRING function.

select SUBSTRING(flda,CHARINDEX('|',flda),CHARINDEX('|',flda)-CHARINDEX('|',flda)-1)  from #owner
thayduckProgrammer AnalystAuthor Commented:
First test record contains below:

1234|terry|5678

Want to get Terry.
plusone3055Commented:
SELECT 
	REPLACE(LEFT(SUBSTRING(flda,CHARINDEX('|',flda)+1
	,LEN(flda)),CHARINDEX('|',SUBSTRING(flda,CHARINDEX('|',flda)+1
	,LEN(flda))))
	,'|'
	,'')
FROM YourTableHere

Open in new window


Solution

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
thayduckProgrammer AnalystAuthor Commented:
Dug around and found below code that also works. You were close. You other solution works perfectly...

SELECT SUBSTRING(Name1,CHARINDEX('|',Name1)+1, CHARINDEX('|',Name1,CHARINDEX('|',Name1)+1)-CHARINDEX('|',Name1)-1) from #owner  

Hey, 2 for the price of one..

Thanks for your help...
plusone3055Commented:
NP
I was trying to do it from my head
when I actually took 15 min to make a dummy table with your data in SQL server i got it.
No worries
glad it helped
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.