查询:
SELECT filePath FROM Table WHERE filePath IS NOT NULL
它会返回\\server\folder1\folder2\filename.tif
我需要查询它以替换" \\server\folder1\folder2\
"使用存储过程中的变量(@Path
)并结束文件的格式,如.jpg。
所以结果会像@Path +' .jpg'
我将如何做到这一点?
答案 0 :(得分:2)
您可以使用REVERSE
,SUBSTRING
,LEFT
和CHARINDEX
等字符串函数的组合:
CREATE TABLE YourTable(filePath VARCHAR(2000))
INSERT INTO YourTable VALUES('\\server\folder1\folder2\filename.tif');
DECLARE @path VARCHAR(2000) = 'path\to\folder\'
SELECT
[File] = REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath),0) - 1)),
[File without ext] =
SUBSTRING(
REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath), 0) - 1)),
0,
CHARINDEX(
'.',
REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath), 0) - 1)),
0
)
),
[Final String] =
@path +
SUBSTRING(
REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath), 0) - 1)),
0,
CHARINDEX(
'.',
REVERSE(LEFT(REVERSE(filePath), CHARINDEX('\', REVERSE(filePath), 0) - 1)),
0
)
) +
'.jpg'
FROM YourTable
答案 1 :(得分:0)
使用REVERSE
和CHARINDEX
的组合来找到最后一个\
:
DECLARE @filePath VARCHAR(500) = '\\server\folder1\folder2\filename.tif'
DECLARE @newPath VARCHAR(500) = '\\server2\newpath\'
SELECT @newPath +
SUBSTRING(@filePath, LEN(@filePath) - CHARINDEX('\', REVERSE(@filePath))+2, LEN(@filePath))