Solved

Searching for a directory in a path and returning the sub directory

Posted on 2013-02-05
4
284 Views
Last Modified: 2013-02-21
Hey folks,

I'm trying to return the sub directory of a path parse, basically a list of user folders beneath \documents and settings\user, but \documents and settings\ could exist anywhere in the path.

Using SQL Server 2008.

So the steps are:

1) search for \documents and settings\
2) return everything before the next slash, aka \documents and settings\merv11\

Right now I'm doing something like this:

SELECT SUBSTRING(filepath, 1, CHARINDEX('\documents and settings\', filepath)) FROM dbo.directory

but it doesn't work out how I thought it would.
0
Comment
Question by:merv11
4 Comments
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 350 total points
ID: 38856930
If I understand you correctly, you have a database field that might have a value like
"C:\abcdefg\documents and settings\merv11\xyz"
and you want
"\documents and settings\merv\"

So the real key is to find where '\documents and settings\' starts, and where the 1st '\' after this string is located.

I'm having problems getting my SQL Server running at the moment, so I can't test the following, but see what you get.

SUBSTRING(filepath,CHARINDEX('\documents and settings\',filepath), 24 + CHARINDEX('\',SUBSTRING(filepath,CHARINDEX('\documents and settings\',filepath)+24))))

The idea is that the inner most SUBSTRING is extracting everything from filepath that comes after '\documents and settings\'.  
SUBSTRING(filepath,CHARINDEX('\documents and settings\',filepath)+24)) => "merv11\xyz"

The CHARINDEX that surrounds this inner most SUBSTRING then finds the first '\' in this substring.
CHARINDEX('\',"merv11\xyz") => 7

The 1st CHARINDEX inside the outer most SUBSTRING find the start of the desired substring.
CHARINDEX('\documents and settings\',filepath) => 11

The outer most SUBSTRING gets the desired value by putting this all together.
SUBSTRING(filepath, 11, 24 + 7).

Off the top of my head, I don't know if CHARINDEX is 0 based or 1 based.  So things might be off by one character (so you might need to make the length of the outer-most substring be 25 + where the next '\' is found rather than 24).
0
 
LVL 39

Assisted Solution

by:appari
appari earned 75 total points
ID: 38857821
try this

declare @path varchar(100)
select @path='c:\users\test\documents and settings\merv11\'

select '\documents and settings' + reverse(left(reverse(@path), charindex(reverse('\documents and settings\'),reverse(@path))))
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 75 total points
ID: 38860298
SELECT
    SUBSTRING(path, CHARINDEX('\documents and settings\', path),
        CHARINDEX('\', path, CHARINDEX('\documents and settings\', path) + 24) - CHARINDEX('\documents and settings\', path) + 1)
FROM (
    SELECT 'c:\documents and settings\merv11\mydocuments\text1.txt' AS path UNION ALL
    SELECT 'c:\some_other_folder\documents and settings\merv11\personal_stuff\personal_xls.xls'    
) AS test_data
WHERE
    path LIKE '%\documents and settings\%'
0
 

Author Comment

by:merv11
ID: 38915794
Apologies for the long response, I left on a trip and totally forgot about this. That worked well, thanks guys!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now