By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE test (
unc_file_path VARCHAR(256)
)
INSERT INTO test SELECT '\\servername\folder1\FTP\folder2/111\862450_FileBundle.zip';
INSERT INTO test SELECT '\\servername\folder1\FTP\folder2\222/862450_FileBundle.zip';
INSERT INTO test SELECT '\\servername\folder1\FTP\folder2\333\862450_FileBundle.zip';
INSERT INTO test SELECT '\\servername\folder1\FTP\folder2/444/862450_FileBundle.zip';
INSERT INTO test SELECT '\\servername\folder1\FTP\folder2\xxx/862450_FileBundle.zip';
5 rows affected
SELECT
*,
SUBSTRING(test.unc_file_path, headMatch.pos+1, headMatch.chars)
FROM
test
OUTER APPLY
(
SELECT
MIN(pos), MIN(chars)
FROM
(
SELECT
PATINDEX('%' + head + body + tail + '%', test.unc_file_path) AS pos, chars
FROM
(
SELECT '\'
UNION ALL SELECT '/'
)
head(head)
CROSS JOIN
(
SELECT 1, '[0-9]'
UNION ALL SELECT 2, '[0-9][0-9]'
UNION ALL SELECT 3, '[0-9][0-9][0-9]'
UNION ALL SELECT 4, '[0-9][0-9][0-9][0-9]'
UNION ALL SELECT 5, '[0-9][0-9][0-9][0-9][0-9]'
)
body(chars, body)
CROSS JOIN
(
SELECT '\'
UNION ALL SELECT '/'
)
tail(tail)
)
match
unc_file_path | pos | chars | (No column name) |
---|---|---|---|
\servername\folder1\FTP\folder2/111\862450_FileBundle.zip | 33 | 3 | 111 |
\servername\folder1\FTP\folder2\222/862450_FileBundle.zip | 33 | 3 | 222 |
\servername\folder1\FTP\folder2\333\862450_FileBundle.zip | 33 | 3 | 333 |
\servername\folder1\FTP\folder2/444/862450_FileBundle.zip | 33 | 3 | 444 |
\servername\folder1\FTP\folder2\xxx/862450_FileBundle.zip | null | null | null |