sql server - Replacing a string before the last "\" character in SQL query -
query:
select filepath table filepath not null
it'll return \\server\folder1\folder2\filename.tif
i need query replace "\\server\folder1\folder2\
" variable that's in stored procedure (@path
) , end format of file .jpg.
so result @path + '.jpg'
how go doing this?
you can use combination of string functions reverse
, substring
, left
, charindex
:
create table yourtable(filepath varchar(2000)) insert 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' yourtable