Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting certain data from a string

Posted on 2016-11-08
1
Medium Priority
?
68 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

604 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