Search code examples
sql-serverazure-sql-database

How to dynamically replace token based on pattern


I'm building an ETL that's newest ingestion client delivers a file a day with today's year month day build into the filename. I'd like the process to replace the filename on the left with the filename on the right. For the resulting Filename I'm basing it off of today's date of 2023/02/23. I'm not sure how to build the parameterized filename that includes offset and type. For row 1 I'm thinking something like this: Data_{[yyyy-MM-dd][0][date]}.json but hoping someone has a better idea because it just feels wrong to me. The replacement type of date by itself would help, adding in replace and regex options would add additional flexibility. My question is how would I replace the parameterized file name with the resulting filename in a dynamic extendable way?

Parameterized File Name Resulting File Name Offset Type Matching string Replacement string
Data_{yyyy-MM-dd}.json Data_2023-02-23.json 0 date
Data_{yyyy-MM}.json Data_2023-02.json 0 date
Data_{MMMM}.json Data_February.json 0 date
Data_{MMMM}.json Data_January.json -24 date
Data_{yyyy-MM-dd}.json Data_2023-02-23.json N/A replace _ -
Data Data File Name Name.json Data File Name.json N/A regex ([a-zA-Z]+) \1 $1

Solution

  • Here is what will give a jumpstart.

    SQL

    -- DDL and sample data population, start
    DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, [FileName] NVARCHAR(MAX));
    INSERT @tbl (FileName) VALUES
    ('Data_{yyyy-MM-dd}.json'), 
    ('Data_{yyyy-MM}.json'),            
    ('Data_{MMMM}.json'),           
    ('Data_{MMMM}.json'),           
    ('Data_{yyyy-MM-dd}.json'), 
    ('Data Data File Name Name.json');
    -- DDL and sample data population, end
    
    DECLARE @today DATETIME = GETDATE();
    
    SELECT *
        , Result = LEFT([FileName], [start] - 1) + 
            FORMAT(@today, fmt) + 
            RIGHT([FileName], LEN([FileName]) - [end])
    FROM @tbl
    CROSS APPLY (SELECT CHARINDEX('{', [FileName]), CHARINDEX('}', [FileName])) AS t1([start], [end])
    CROSS APPLY (SELECT SUBSTRING([FileName], [start] + 1, [end] - [start] - 1)) AS t2(fmt)
    WHERE [FileName] LIKE '%[{}]%';
    

    Output

    ID FileName start end fmt Result
    1 Data_{yyyy-MM-dd}.JSON 6 17 yyyy-MM-dd Data_2023-02-23.JSON
    2 Data_{yyyy-MM}.json 6 14 yyyy-MM Data_2023-02.json
    3 Data_{MMMM}.json 6 11 MMMM Data_February.json
    4 Data_{MMMM}.json 6 11 MMMM Data_February.json
    5 Data_{yyyy-MM-dd}.json 6 17 yyyy-MM-dd Data_2023-02-23.json