January 8, 2021 at 6:14 am
I'm new to the forum and have a quick question.
Sample data set from "SELECT path from invoices"
temp/a/g.txt
test/b/t-t.txt
windows/c/45_4.txt
Is there a way native to SQL to update the above results to be:
temp/a/this.bat
test/b/this.bat
windows/c/this.bat
January 8, 2021 at 11:50 am
It's not pretty, or fast, but it works:
SELECT
dat.pth
, path2 = STUFF(dat.pth, calc.pos2, calc.pos1 - calc.pos2, 'this')
FROM
(
VALUES ('temp/a/g.txt')
, ('test/b/t-t.txt')
, ('windows/c/45_4.txt')
, ('ksjdch/sdgsfdg/sgsdg/sdsd/sdgsdgs/fred.longextension')
) dat (pth)
CROSS APPLY
(SELECT rev = REVERSE(dat.pth), lgth = LEN(pth)) r1
CROSS APPLY
(
SELECT
pos1 = r1.lgth - CHARINDEX('.', r1.rev) + 1
, pos2 = r1.lgth - CHARINDEX('/', r1.rev) + 2
) calc;
January 8, 2021 at 5:12 pm
SELECT
path AS original_path,
LEFT(path, LEN(path) - CHARINDEX('/', REVERSE(path)) + 1) + 'this.bat'
FROM
(
VALUES ('temp/a/g.txt')
, ('test/b/t-t.txt')
, ('windows/c/45_4.txt')
, ('ksjdch/sdgsfdg/sgsdg/sdsd/sdgsdgs/fred.longextension')
) AS invoices (path)
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
January 8, 2021 at 5:26 pm
Blimey, I think I need to stop posting and see a doctor! For some reason, I thought that there was a need to maintain the original file extension.
January 10, 2021 at 5:35 am
Thanks for the replies. This works like a charm.
SELECT
path AS original_path,
LEFT(path, LEN(path) - CHARINDEX('/', REVERSE(path)) + 1) + 'this.bat'
FROM
(
VALUES ('temp/a/g.txt')
, ('test/b/t-t.txt')
, ('windows/c/45_4.txt')
, ('ksjdch/sdgsfdg/sgsdg/sdsd/sdgsdgs/fred.longextension')
) AS invoices (path)
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply