我的表格中有文件位置,如
FileLocation: - " \ Saurabh \ Rahul \ Saurabh \ ABC.text"
我需要更换" Saurabh"与" Ramesh"但我需要先取代" Saurabh"只说不是所有的" saurabh"这是在字符串中。
我试过
select REPLACE(FILELOCATION,'saurabh','Ramesh')
我怎么能实现?
答案 0 :(得分:1)
试试这个:
DECLARE @str varchar(100) = '\Saurabh\Rahul\Saurabh\ABC.text'
SELECT STUFF(@str, CHARINDEX('saurabh', @str), LEN('saurabh'), '')
<强>输出:强>
\\Rahul\Saurabh\ABC.text
答案 1 :(得分:1)
要仅替换字符串中单词的第一个出现,您可以将其写为:
DECLARE @FileLocation VARCHAR(MAX)
DECLARE @ReplaceSubString VARCHAR(MAX),@NewSubString VARCHAR(MAX)
SET @FileLocation = '\\Saurabh\Rahul\Saurabh\ABC.text'
SET @ReplaceSubString = 'Saurabh'
SET @NewSubString = 'Ramesh'
SELECT STUFF ( @FileLocation ,
CHARINDEX(@ReplaceSubString, @FileLocation) ,
Len(@ReplaceSubString) ,
@NewSubString
)