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 8198777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:01:24+00:00 2026-06-07T06:01:24+00:00

A column in one my SQL Server database tables contains paths (not file paths,

  • 0

A column in one my SQL Server database tables contains paths (not file paths, just the directory path).

I currently have a query which returns all sub-folders of a given file path, using the LIKE function;

WHERE Path LIKE @FilePath + '%'

On a side note, I am aware that there is an issue with this approach as the escape characters used by LIKE are all valid file path characters. I’m going to make proper use of ESCAPE in the finished query.

Anyway, back to my question. I would like to be able to modify the query so that it returns only direct sub-folders of the specified path. This isn’t as easy as just putting a trailing slash on the end of the string, as obviously the % wildcard gobbles everything up as it parses the right portion of the string.

I was looking at using the [^] escape sequence, to perhaps do a search which excludes the \ character, but I’ve had no luck with that. I tried this;

WHERE Path LIKE @FilePath + '[^\]'

But it doesn’t produce the intended result, so I’m not even sure if I’m using it correctly.

  • 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-07T06:01:26+00:00Added an answer on June 7, 2026 at 6:01 am

    Do not use LIKE for this. LEFT and SUBSTRING will give better performance as you are not doing any wildcard searches, which you should try and avoid in all instances.

    The query below will do this and exclude an exact match, so if there is a directory ‘C:\Temp\ it will not show up in the results, but all the sub directories will.

    What we are doing is throwing away all results that possibly have more sub directories (‘\’) and just using the ones with a single sub directory.

    Hope this helps!

    DECLARE @directory VARCHAR(150);
    SET @directory = 'C:\Temp\';
    
    SELECT 
        * 
    FROM 
        tblDir
    WHERE 
        LEFT(Dir, LEN(@directory)) = @directory
        AND CHARINDEX('\', SUBSTRING(Dir, LEN(@directory) ,LEN(Dir) - LEN(@directory)), 2) = 0
        AND Dir <> @directory
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL Server 2008 database, which contains about 40 tables. Most of
I have the requirement to search several different tables in my SQL Server database.
I have 3 tables in my SQL Server database: Teacher Student Parent Each of
I have two tables( Table1 and Table2 ) in a SQL Server database and
I'm using SQL Server and in one of the tables I have a nullable
I have a table in my sql server 2005 database which contains about 50
I have MS SQL Server database and insert some values to one of the
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Why does Sql server doesn't allow more than one IDENTITY column in a table??
I would like to have one column in QTableWidget NOT editable. In forums I

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.