Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

How do I select only the value that is between parenthesis

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
jamesh1031
Asked:
jamesh1031
1 Solution
 
zveljkovicCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now