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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Normalizing Data

Author  Topic 

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-02-02 : 14:50:25
How can i pull the name out of a path?

A combintation of string, charindex???

example:

[code]
\\namehere\sometext-moretext
[\code]

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-02 : 14:54:34
Declare @path varchar(max)
SET @path = '\\namehere\sometext-moretext'

Select SUBSTRING(@path,3,charindex('\',@path,3)-3)




Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-02-02 : 15:14:20
That is working! THANKS!!! But i'm still a little bit confused on exactly what's going on here....
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-02 : 15:35:45
in english

charindex('\',@path,3) = "find me the specified character in the string, start looking in the 3rd position so I ignore the 1st two"

SUBSTRING(@path,3,charindex('\',@path,3)-3) = "Give me the middle of the specified string, starting at position 3 and for the number of characters until the position where the '\' is found, minus 3 (since we are starting at position 3)

your welcome.




Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-02-02 : 16:23:28
Great explanation! I knew what the
charindex('\',@path,3)
part was doing, but it was getting confused when nesting that in the substring.
Go to Top of Page
   

- Advertisement -