Results 1 to 4 of 4

Thread: Removing a set of string from a string

  1. #1
    Join Date
    Oct 2010
    Posts
    6

    Removing a set of string from a string

    Hi,

    I have a table name DS_MEDIA_MAPPING.
    The column 'MEDIA_PATH_PREFIX' contains paths like below:

    C:\Documents and Settings\admin\My Documents\media\ABC001\
    C:\Documents and Settings\admin\My Documents\media\SOS001\DESK\
    C:\Documents and Settings\admin\MEDIA\FTP001\READ_ME.txt
    .
    .
    .

    Here, I have to get the folder name after the folder name media. In the above example, I need :
    ABC001
    SOS001
    FTP001

    I ran the following query :

    Select (Substring(MEDIA_PATH_PREFIX, PATINDEX('%\media\%', MEDIA_PATH_PREFIX) + 7, LEN(MEDIA_PATH_PREFIX)))
    FROM DS_MEDIA_MAPPING


    This query gives me all the characters after '\media\'.

    Please help me in removing all the characters after:
    ABC001
    SOS001
    FTP001

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    Use REVERSE function to reverse the string and find first /, then subtract position from length of the script to get position of /, then use substring to extract the string you want.

  3. #3
    Join Date
    Oct 2010
    Posts
    6
    I tried but am not getting desired one.
    Could anyone please write it for me?

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    I have not tested it, but it should be something like this

    Select (Substring(MEDIA_PATH_PREFIX, PATINDEX('%\media\%', MEDIA_PATH_PREFIX) + 7, LEN(MEDIA_PATH_PREFIX)-patindex('\',reverse(media_path_prefix)))
    FROM DS_MEDIA_MAPPING

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •