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
 Extracting Part of a String

Author  Topic 

jsu
Starting Member

2 Posts

Posted - 2010-03-03 : 20:44:15
Hi,

I am trying to extract data from a string field. The data I am after is contained within parentheses, which in turn is embedded within a larger string (i.e. xxxxx(yyyy)xxxx where the data I'm trying to extract is yyyy. Both the yyyy and xxxx are variable lengths.

I understand that it is possible to use chindex and right function to do this, but not sure about the correct syntax. Can anyone help?

Regards,

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-03-04 : 00:05:35
declare @a varchar(50)
set @a='xxxxx(yyyyyyyyyyyy)xxxx'

select SUBSTRING(@a,CHARINDEX('(',@a)+1,(CHARINDEX(')',@a)-CHARINDEX('(',@a)-1))

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-04 : 02:02:12
If the data donesnot have a dot,


declare @a varchar(50)
set @a='xxxxx(yyyyyyyyyyyy)xxxx'
select parsename(replace(replace(@a,'(','.'),')','.'),2)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jsu
Starting Member

2 Posts

Posted - 2010-03-10 : 13:47:03
Thank you both very much! This worked.
Go to Top of Page
   

- Advertisement -