Solved

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

Posted on 2013-02-05
4
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

728 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