Solved

Getting certain data from a string

Posted on 2016-11-08
1
34 Views
Last Modified: 2016-11-08
I have a string in the DB

"JRSJC2LPValueSummary(122012)pdf(1179185).pdf"  

I need to get the number between the ().  If there is more than one set of (), like in this example, then I need the number from the 2nd set of ().

1179185

I can get the first set by using this:

substring([FileName], charindex('(', [FileName]) + 1, charindex(')', [FileName]) - charindex('(', [FileName]) - 1) AS DocumentNumberParsed

I don't know how to get the 2nd set IF there is a 2nd set.

I appreciate any help.
0
Comment
Question by:huerita37
1 Comment
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 41879577
SELECT [FileName], SUBSTRING([FileName], FileName_Lparen_Final + 1,
    CHARINDEX(')', STUFF([FileName], 1, FileName_Lparen_Final + 1, ''))) AS FileNameNumber
FROM (
    VALUES('JRSJC2LPValueSummary(122012)pdf(1179185).pdf'),
                  ('JRSJC2LPValueSummary(122012).pdf')
) AS test_data([FileName])
CROSS APPLY (
    SELECT CHARINDEX('(', [FileName]) AS FileName_Lparen1
) AS ca1
CROSS APPLY (
    SELECT CHARINDEX('(', [FileName], FileName_Lparen1 + 1) AS FileName_Lparen2
) AS ca2
CROSS APPLY (
    SELECT CASE WHEN FileName_Lparen2 > 0 THEN FileName_Lparen2 ELSE FileName_Lparen1 END AS FileName_Lparen_Final
) AS ca3
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now