Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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

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
merv11
Asked:
merv11
3 Solutions
 
HooKooDooKuCommented:
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
 
appariCommented:
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
 
Scott PletcherSenior DBACommented:
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
 
merv11Author Commented:
Apologies for the long response, I left on a trip and totally forgot about this. That worked well, thanks guys!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now