英文:
How to dynamically replace token based on pattern
问题
| 参数化文件名 | 结果文件名 | 偏移 | 类型 | 匹配字符串 | 替换字符串 |
|:-----: |:------:|:-----:|:-----:|:-----:|:-----:|
| Data_{yyyy-MM-dd}.json | Data_2023-02-23.json| 0 | 日期 |
| Data_{yyyy-MM}.json | Data_2023-02.json| 0 | 日期 |
| Data_{MMMM}.json | Data_February.json| 0 | 日期 |
| Data_{MMMM}.json | Data_January.json| -24 | 日期 |
| Data_{yyyy-MM-dd}.json | Data_2023-02-23.json| 无 | 替换 | _ |-|
| Data Data File Name Name.json | Data File Name.json| 无 | 正则表达式 |([a-zA-Z]+) \|$1|
英文:
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 |
答案1
得分: 1
这是一个启动的好跳板。
SQL
-- DDL 和样本数据填充,开始
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 和样本数据填充,结束
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 |
英文:
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 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论