Extract a uniform pattern from a file path

  • Hi All

    I have a strange problem here which I can't quite get to the bottom of.

    I have a file path in my table which reads like this:

    \\cc0dware01\BMF\RMS\REBATERATES-WORKING-2016ToEnd08-20160905180112.csv

    I only want to extract 20160905180112 from the string, the name format is strict and will always be the same.

    Despite trying many things LEFT, REVERSE, CHARINDEX and getting very close I can't quite pull out the part I want every time.

    Anyone got an easy fix for this?

    Any help greatly appreciated.

  • pnr8uk (10/10/2016)


    Hi All

    I have a strange problem here which I can't quite get to the bottom of.

    I have a file path in my table which reads like this:

    \\cc0dware01\BMF\RMS\REBATERATES-WORKING-2016ToEnd08-20160905180112.csv

    I only want to extract 20160905180112 from the string, the name format is strict and will always be the same.

    Despite trying many things LEFT, REVERSE, CHARINDEX and getting very close I can't quite pull out the part I want every time.

    Anyone got an easy fix for this?

    Any help greatly appreciated.

    Quick suggestion

    😎

    DECLARE @INPUT_STR VARCHAR(1024) = '\\cc0dware01\BMF\RMS\REBATERATES-WORKING-2016ToEnd08-20160905180112.csv';

    SELECT

    LEFT(RIGHT(@INPUT_STR,CHARINDEX(CHAR(45),REVERSE(@INPUT_STR),1) - 1),14) AS OUT_STR;

    Output

    OUT_STR

    --------------

    20160905180112

  • Wow thank you that is amazingly simple.

    Many thanks again

  • You are very welcome.

    😎

  • This might be is simpler.

    DECLARE @INPUT_STR VARCHAR(1024) = '\\cc0dware01\BMF\RMS\REBATERATES-WORKING-2016ToEnd08-20160905180112.csv';

    SELECT SUBSTRING( @INPUT_STR, LEN( @INPUT_STR) - 17, 14);

    I wonder what are the varying parts of the file path. Is it only the path? Maybe WORKING can change to something else? Or maybe the time period? Knowing what can change, we can be sure to prevent any problems.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Looks like a time code (fixed at 14 characters)... The following will work even if the file path changes...

    DECLARE @input VARCHAR(1000) = '\\cc0dware01\BMF\RMS\REBATERATES-WORKING-2016ToEnd08-20160905180112.csv';

    SELECT output = LEFT(RIGHT(@input, 18), 14);

  • As long as the file name is the only YYYYMMDDHHMMSS string in the file, I think you can just check for that pattern:

    SELECT input, SUBSTRING(input, PATINDEX('%[2][01][0-9][0-9][01][0-9][012][0-9][0-5][0-9][0-5][0-9]%', input), 14) AS extract

    FROM (

    VALUES('\\cc0dware01\BMF\RMS\REBATERATES-WORKING-2016ToEnd08-20160905180112.csv'),

    ('\\cc0dware01\BMF\RMS\REBATERATES-WORKING-2016ToEnd08-20160905180113.xlsx'),

    ('c:\20160905180113.')

    ) AS test_data(input)

    Edit: Added SQL code block.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply