Solved

Sql to Replace Folderpath string in MS Access Table field

Posted on 2016-11-30
7
13 Views
Last Modified: 2016-11-30
Hi Experts,

    I have an MS Access Table MYTable   which has the field MyPath

      The Sample entries in MyPath  (The values corresponding to a  folder structure that  has several subfolders
           \\server\Root\1\file1.pdf
           \\server\Root\2\file2.pdf
           \\server\Root\2\SubFold\file3.doc

         I need the  SQL to  modify the entries in  the MYPath  and all files will correspond to the root folder
             The Entries after modification will look

          \\server\Root\file1.pdf
          \\server\Root\file2.pdf
          \\server\Root\file3.doc

           All I need is the SQL  to modify the entries ( And not the actual physical moving of files)
0
Comment
Question by:Sam OZ
7 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
will this good enough for you?

SELECT 
MyPath, "\\server\Root" & mid(MyPath, instrrev(MyPath, "\")) as newField
FROM MYTable;

Open in new window

0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
or you can try to create a self-defined function:

Function generatePath(ByVal path As String, Optional ByVal SubDirectoryLevel As Integer = 2)
    Arr = Split(path, "\")
    If UBound(Arr) <= SubDirectoryLevel + 1 Then
        generatePath = ""
    Else
        For i = 0 To SubDirectoryLevel + 1
            generatePath = generatePath & Arr(i) & "\"
        Next
        generatePath = generatePath & Mid(path, InStrRev(path, "\") + 1)
    End If
End Function

Open in new window

then call your query like this:
SELECT 
MyPath, 
"\\server\Root" & mid(MyPath, instrrev(MyPath, "\")) as newField,
generatePath(myPath) as newField2
FROM MYTable;

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Why is \\server\Root\2\SubFold\file3.doc not changed to \\server\Root\SubFold\file3.doc ?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 30

Expert Comment

by:hnasr
Comment Utility
Try this:
In a module:
Function SplitPath(varString As String)
    Dim b() As String
    b = Split(varString, "\")
    If UBound(b) > 2 Then
     SplitPath = "\\" & b(2) & "\" & b(3) & "\" & b(UBound(b))
    End If
End Function

Open in new window

In query:
SELECT SplitPath(MyPath) FROM MYTable ;

Open in new window

0
 

Author Comment

by:Sam OZ
Comment Utility
Hi All,
    I am looking  SQL to modify the field ... Not Select statements
    ( VBA routines is also fine) . Finally the values in the field need to be changed as per the rule

    All the  files are in Root path . So folder paths are changed accordingly
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
I am looking  SQL to modify the field ... Not Select statements
do you mean using Update statement instead?

if yes, make sure you backup your data and then try like this:
Update MYTable Set MyPath = "\\server\Root" & mid(MyPath, instrrev(MyPath, "\"))

Open in new window

Or
Update MYTable Set MyPath = generatePath(MyPath)

Open in new window

0
 
LVL 30

Expert Comment

by:hnasr
Comment Utility
The field in query is modified as per rule. To update table:
UPDATE MYTABLE SET MyPath = SplitPath(MyPath)

Open in new window

Use with the following function in a module (repeated from previous comment):
Function SplitPath(varString As String)
    Dim b() As String
    b = Split(varString, "\")
    If UBound(b) > 2 Then
     SplitPath = "\\" & b(2) & "\" & b(3) & "\" & b(UBound(b))
    End If
End Function

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now