Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:26:25+00:00 2026-06-14T11:26:25+00:00

I have a string (which represents a folder path). The max number of levels

  • 0

I have a string (which represents a folder path).
The max number of levels is known (subsubfolder)
The min number of levels is known (root does not contain files).
The foldernames can contain spaces.
There is no indication whether a in the example is a folder or a file………….
I want that string to be split into columns.

CREATE TABLE TESTDATA([path] [nvarchar](max))
INSERT INTO TESTDATA (path) VALUES (N'/a/)
INSERT INTO TESTDATA (path) VALUES (N'/a/ab/filename1)
INSERT INTO TESTDATA (path) VALUES (N'/a/ab/abc/filename2)
INSERT INTO TESTDATA (path) VALUES (N'/a/ab/abc/filename3)
INSERT INTO TESTDATA (path) VALUES (N'/a/ab/abc/abcd/filename4)
INSERT INTO TESTDATA (path) VALUES (N'/a/ac/ac e/filename5)

TESTDATA now looks like

|----------path-----------------|
/a
/a/ab/filename1
/a/ab/abc/filename2
/a/ab/abc/filename3
/a/ab/abc/abcd/filename4
/a/ac/ac e/filename5

but I need it to look like

filename--|--root--|--folder--|--subfolder--|--subsubfolder--|
----------|--------|----------|-------------|----------------|
filename1-|---a----|----ab----|
filename2-|---a----|----ab----|-----abc-----|
filename3-|---a----|----ab----|-----abc-----|
filename4-|---a----|----ab----|-----abc-----|-------abcd-----|
filename5-|---a----|----ac----|-----ac e----|

How would I go about this?

I am thinking I should do something with SUBSTRING and CHARINDEX but I tried a thousand things; never getting it right. It seems SUBSTRING stops searching in a string when that string contains spaces.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:26:26+00:00Added an answer on June 14, 2026 at 11:26 am

    Using CHARINDEX and SUBSTRING is a good approach. Remember that CHARINDEX can take a third parameter that specifies the starting point for searching. Using this, you can find the second /, and third /, and so forth. One way to break down the problem is to use subqueries that build on each other and attack one small piece of the problem at a time. Here is an example that decomposes the filenames into subsub folder level using successive Common-Table Expressions:

    with root as (
      select
        path,
        left(path, charindex('/', path, 2) - 1) as root,
        substring(path, charindex('/', path, 2), 1000) as rest
      from testdata
    ), folder as (
      select
        path,
        root,
        case when charindex('/', rest, 2) > 0
          then left(rest, charindex('/', rest, 2) - 1)
          else '' 
        end as folder,
        case when charindex('/', rest, 2) > 0
          then substring(rest, charindex('/', rest, 2), 1000)
          else rest
        end as rest
      from root
    ), subfolder as (
      select
        path,
        root,
        folder,
        case when charindex('/', rest, 2) > 0
          then left(rest, charindex('/', rest, 2) - 1)
          else '' 
        end as subfolder,
        case when charindex('/', rest, 2) > 0
          then substring(rest, charindex('/', rest, 2), 1000)
          else rest
        end as rest
      from folder
    ), subsubfolder as (
      select
        path,
        root,
        folder,
        subfolder,
        case when charindex('/', rest, 2) > 0
          then left(rest, charindex('/', rest, 2) - 1)
          else '' 
        end as subsubfolder,
        case when charindex('/', rest, 2) > 0
          then substring(rest, charindex('/', rest, 2), 1000)
          else rest
        end as rest
      from subfolder
    )
    select
      path,
      rest as filename,
      root,
      folder,
      subfolder,
      subsubfolder
    from subsubfolder;
    

    Demo: http://www.sqlfiddle.com/#!3/e91eb/36

    Sample Output:

    PATH                      FILENAME    ROOT  FOLDER  SUBFOLDER  SUBSUBFOLDER
    ------------------------  ----------  ----  ------  ---------  ------------  
    /a/                       /           /a               
    /a/ab/filename1           /filename1  /a    /ab          
    /a/ab/abc/filename2       /filename2  /a    /ab     /abc     
    /a/ab/abc/filename3       /filename3  /a    /ab     /abc     
    /a/ab/abc/abcd/filename4  /filename4  /a    /ab     /abc       /abcd
    /a/ac/ac e/filename5      /filename5  /a    /ac     /ac e
    

    Note: Removing the / that I’ve kept in as part of the folder name can be trivially done with SUBSTRING(part, 2, 1000) or SUBSTITUTE(part, '/', '').

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string variable which represents the full path of some file, like: x
I have a string which represents a binary number: 1010, which represents a 10(ten)
Not sure if this is the best approach but I have some string which
I have a string which represents a multidimensional array in the format: [[A, a],
I have a String 20120119 which represents a date in the format 'YYYYMMDD' .
I have a string, which represents part of xml. string text =word foo<tag foo='a'
Here I have string like this 1322485986.672901000 , data type is string which represents
I have a string which represents a DataTime value, and I want to workout
I have a string in PHP (came from some data source), which represents a
I have a List<HashMap<String,Object>> which represents a database where each list record is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.