Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Delete from the string

Author  Topic 

a.ashabi
Posting Yak Master

117 Posts

Posted - 2009-11-05 : 14:56:02
Hi.I'd like to delete everything after the last "-" found in the string.
I mean if I have these fields:

L-Arginine 500 mg - 100 Capsules
L-Carnitine 250 mg - 30 Caps
SAMe 400 mg Vegetarian - 30 Tablets
Pro-GH#8482; - 600 g (21.2 oz.)
L-Arginine Powder - 1 lb.
.
.
.

I need to get these results:

L-Arginine 500 mg
L-Carnitine 250 mg
SAMe 400 mg Vegetarian
Pro-GH#8482;
L-Arginine Powder
.
.
.

please help.
Thanks in advanced




jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-11-05 : 15:03:43
DECLARE @string varchar(100)
set @string = 'L-Arginine 500 mg - 100 Capsules'

select reverse(right(reverse(@string),len(@string) - charindex( '-', reverse(@string))) )


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-05 : 15:03:58
Is it always 2 dashes, or can there be more



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-05 : 15:05:41
Well that seems to work nicely


DECLARE @string varchar(100)

set @string = 'L-Arginine 500 mg - 100 Capsules'
select reverse(right(reverse(@string),len(@string) - charindex( '-', reverse(@string))) )

set @string = 'L-Arginine 500 mg - 100 Capsules - Bretts Tequila'
select reverse(right(reverse(@string),len(@string) - charindex( '-', reverse(@string))) )




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

a.ashabi
Posting Yak Master

117 Posts

Posted - 2009-11-05 : 15:14:41
thank u so much
it worked :)
Go to Top of Page
   

- Advertisement -