Sql server query to get Filename from Filepath

I need an sql query for Sql server 2014

 I have fileds DocNo ,Docpath and more fields on a Docs table

    DocPath field currently has relative path like  \mypath\fold1\subfold1\mydoc.docx  . I need to have the value only as just the file name

 sample existting values

  I should get the values modified as
Sam OZAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this code:
select right(TableDocs.DocPath, charindex('\', reverse(TableDocs.DocPath) + '\') - 1) from TableDocs

Open in new window

ste5anSenior DeveloperCommented:
Use the correct domains. Thus store the path separate from the filename and extension. Even the extension should be separated, when you need to query it somewhere. Cause your current column is not atomic, violating 1NF.

And consider a user using Linux. Then the path delimiter is /.
Sam OZAuthor Commented:
Hi Misha. I am looking for update not select
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

ste5anSenior DeveloperCommented:
Just a comment: This would be a further code smell/design flaw. Storing a filename in a column designed to store a path..
Try this code to update:
create view DocView as 
select ID, right(TableDocs.DocPath, charindex('\', reverse(TableDocs.DocPath) + '\') -1) as DocPath
 from TableDocs
update TableDocs set  TableDocs.DocPath = DocView.DocPath from DocView
where TableDocs.ID = [DocView].[ID]

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark EdwardsChief Technology OfficerCommented:
@Sam:  Let's not make this anymore complicated than it needs to be.  Misha's formula does what you are looking for, which is to parse the file name from the full path string:


To change a SELECT SQL to an UPDATE SQL, try:

UPDATE [DocsTableName] SET [FilePathFieldName] = Right([DocsTableName].[FilePathFieldName],charindex('\',reverse([DocsTableName].[FilePathFieldName]))-1)

I'll leave it up to you to do whatever other embellishments you want to make to the T-SQL.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.