Solved

How do I select only the value that is between parenthesis

Posted on 2008-06-15
1
182 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

758 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

20 Experts available now in Live!

Get 1:1 Help Now