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
 parse a string

Author  Topic 

strattonm
Starting Member

1 Post

Posted - 2012-11-29 : 14:59:05
Hello.

I need to parse a string by the delimeter "/".

for example, string= /usr/open/windows/whatever/blah/blah.sh
I need to break up the string. I really only need the first 3 levels so in this case, level1 = /usr, level2 = /open, and level3 = /windows.

thanks!

I read the suggested article and the examples are very confusing any other suggestions are appreciated. I prefer a query rather than a procedre because I've never used a procedure before.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-29 : 15:09:15
Using a string splitter such as the one in Jeff Moden's article here would probably be the best way: http://www.sqlservercentral.com/articles/Tally+Table/72993/

Copy the function in Fig 21 of that article and then use it as
SELECT * FROM MASTER.dbo.DelimitedSplit8K(YourStringHere,'/')
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-29 : 19:39:16
Actually, the method I described is probably the simplest. Just follow these steps exactly:

1. Copy the code in Fig. 21 of that article.

2. Open a SSMS query window, set the database to your database from the dropdown, paste the copied code to that window and click execute. Now close that window.

3. Open another SSMS query window and type the query:

declare @str varchar(256) = 'string= /usr/open/windows/whatever/blah/blah.sh';
SELECT * FROM dbo.DelimitedSplit8K(@str,'/');

4. Take a look at the output - it will have the data you need, nicely split.

Give it a try, and you will love it
Go to Top of Page
   

- Advertisement -