SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to get a substring from main string with regex
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

162 Posts

Posted - 12/09/2012 :  15:44:45  Show Profile  Reply with Quote
I have astring like this:
'THIS,IS -12345(abcdefg,DataSource=TheServerName101;XXXLAKSDJNFKAICNCW)'

from that I'd like to extract: TheServerName101, either side of that string could me larger or smaller, thats why I think maybe regex may be better, But I'm not sure how to do that.

Any help appreciated.


Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/09/2012 :  17:29:55  Show Profile  Reply with Quote
Unfortunately there is no native support for regular expressions in T-SQL. There are CLR implementations of regex replace etc. on the web - for example see here

For the example you posted, the following (which uses only native T-SQL functions) should work
DECLARE @x VARCHAR(255) = 'THIS,IS -12345(abcdefg,DataSource=TheServerName101;XXXLAKSDJNFKAICNCW)';
SELECT LEFT(STUFF(@x,1,10+CHARINDEX('DataSource=',@x),''),CHARINDEX(';',STUFF(@x,1,10+CHARINDEX('DataSource=',@x),''))-1);
Go to Top of Page

Starting Member

34 Posts

Posted - 12/10/2012 :  00:22:51  Show Profile  Reply with Quote
I've had similar situation.

This is what I did

Declare @x varchar(255)
Set @x='THIS,IS -12345(abcdefg,DataSource=TheServerName101;XXXLAKSDJNFKAICNCW)'
Select SubString(@x,PatIndex ('%TheServerName101%',@x),16)

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000