Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip from the left 2

Status
Not open for further replies.

Palmyra

Programmer
Jan 5, 2007
150
US
I want to select value 'name' from fieldname instead of value '1) name'. Can someone point me in the right direction?

Thanks.
 
That's going to depend on the content of this field for all records. Will they all be prefixed 'n) '?

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
Yes - 1) name1
2) name2
3) name3

etc.

Thanks.
 
With only the single example you show this is a solution.
Code:
SELECT SUBSTRING(fieldname, 4, LEN(fieldname))
FROM tablename

Since the part you don't want is one number, one close parenthesis, and one space, you want to start at the fourth position. Then you want to copy all the values from the fourth position to the end of the value.

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
...but just in case you have more than a few records, I believe that SQLBill might have been hinting that a (slightly) more general solution would be to accommodate a variable number of digits before the right parenthesis; something along these lines:
Code:
SELECT 
  SUBSTRING(fieldname, CHARINDEX(') ', fieldname, 1) + 2, 1000)  -- 1000: no benefit in calculating remaining length
FROM tablename
Apologies SQLBill if you did not have this in mind. Might be useful anyway, Palmyra.
 
No apologies needed Simon. You just showed another way to do the work. And as I stated my response was based on the information provided - only one digit before the parenthesis. I would still do LEN since I believe 1000 will return extra unneeded spaces.

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top