Solved

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

Posted on 2013-02-05
4
288 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:Scott Pletcher
Scott Pletcher 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Backup & Restore 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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

829 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