Solved

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

Posted on 2013-02-05
4
285 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

895 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

13 Experts available now in Live!

Get 1:1 Help Now