Solved

How do I select only the value that is between parenthesis

Posted on 2008-06-15
1
183 Views
Last Modified: 2010-03-20
The database saves to a field "logonname (Full Name)". I have a field in a select query RequestedBy and I want the results of the query to only return what is between the parenthesis ... Full Name
0
Comment
Question by:jamesh1031
1 Comment
 
LVL 4

Accepted Solution

by:
zveljkovic earned 500 total points
ID: 21788169
Hi. I didn't quite understand You but i think this is what are You looking for:
SELECT substring(
      FieldName,
      CHARINDEX( '(' , FieldName) + 1,
      CHARINDEX(')' , FieldName) - CHARINDEX( '(' , FieldName) - 1
) as FULLNAME
FROM TableName

substring is function which takes 3 parameters:
1. Is expression (string,field,..) from which to return a portion of it
2. is a number of characters from where to start a new string
3. is a number of characters where to end a new string

charindex is a function which returns a position of first parameter within second.
So for 2nd parameter of substring we say find us a parenthesis ( charindex ) and add 1 to that position to not include '(' in result. Similar for 3rd parameter.

NOTE:
1. This was tested in Microsoft Sql Express 2005 (can't guaranty that it will work right away in other DB's, but you could always look for string functions in their documentation)
2. This works only if they are only one open and closed parenthesis in the field.

Hope that helps,
Zlax
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

930 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

10 Experts available now in Live!

Get 1:1 Help Now