在sql server中使用分隔符拆分字符串

时间:2010-04-09 13:03:06

标签: sql-server

我有一个带有varchar字段的coloumn名称,其中包含一些文件夹路径,如“C:\ Program Files \ Internet Explorer \ en-US”。我需要更新根文件夹名称(程序文件到profilesNew)。可以有人请帮忙。 我尝试了一个查询

declare @val as varchar(100)
set @val='C:\Program Files\Internet Explorer\en-US'

select substring(@val,charindex(':\',@val),charindex('\',@val))

但没有得到确切的答案

C:\Program FilesNew\Internet Explorer\en-US

6 个答案:

答案 0 :(得分:1)

试试这个:

declare @val as varchar(100) 
declare @firstSlash int
declare @secondSlash int

set @val='C:\Program Files\Internet Explorer\en-US'

set @firstSlash = charindex('\',@val)
set @secondSlash = @firstSlash + charindex('\', substring(@val,@firstSlash+1,100))

select substring(@val, 1, @secondSlash-1) + 'New' + substring(@val, @secondSlash, 100)

答案 1 :(得分:1)

尝试使用以处理一组路径:

declare @val table (val varchar(100))
INSERT @val VALUES ('C:\Program Files\Internet Explorer\en-US')
INSERT @val VALUES ('C:\My Documents\Internet Explorer\en-US')

SELECT 
    val,LEFT(val,CHARINDEX('\',val,CHARINDEX(':\',val)+3)-1)+'New\'+RIGHT(val,LEN(val)-CHARINDEX('\',val,CHARINDEX(':\',val)+3)) AS New
    FROM @Val

输出:

val                                       New
----------------------------------------- --------------------------------------------
C:\Program Files\Internet Explorer\en-US  C:\Program FilesNew\Internet Explorer\en-US
C:\My Documents\Internet Explorer\en-US   C:\My DocumentsNew\Internet Explorer\en-US

这是一个变量:

declare @val as varchar(100)
set @val='C:\Program Files\Internet Explorer\en-US'

select @val
SELECT LEFT(@val,CHARINDEX('\',@val,CHARINDEX(':\',@val)+3)-1)+'New\'+RIGHT(@Val,LEN(@Val)-CHARINDEX('\',@val,CHARINDEX(':\',@val)+3))

输出:

-------------------------------------------
C:\Program Files\Internet Explorer\en-US


-------------------------------------------
C:\Program FilesNew\Internet Explorer\en-US

答案 2 :(得分:0)

为什么不使用替换?

declare @val as varchar(100) set @val='C:\Program Files\Internet Explorer\en-US'
Select Replace(@Val, 'Program Files', 'Program FilesNew')

这是一个更精致,更有趣的版本,询问这个问题。如果您事先不知道基本路径该怎么办。

Create function fxUpdateBasePath(@Path as varchar(1000), @NewFolder as varchar(100))
returns varchar(1000) as
Begin
    Declare @NewPath as varchar(1000)
    Declare @drive as varchar(10)
    set @Drive = substring(@Path,charindex(':\',@Path) -1,charindex('\',@Path))
    Declare @Root as varchar(100)
    Set @Path = Substring(@Path, len(@Drive) +1, Len(@Path) - Len(@Drive))
    set @Root = Substring(@Path, 1, charindex('\', @Path) -1)
    set @Path = SubString(@Path, Len(@Root) + 1,Len(@Path) - Len(@Root) +1 )
    Set @NewPath = @Drive + @NewFolder + @Path
    return @NewPath
End

Go

    Select dbo.fxUpdateBasePath('C:\Program Files\Internet Explorer\en-US', 'Program FilesNew')
C:\Program FilesNew\Internet Explorer\en-US
    Select dbo.fxUpdateBasePath('C:\Program Files(x86)\Internet Explorer\en-US', 'Program FilesNew')
C:\Program FilesNew\Internet Explorer\en-US

答案 3 :(得分:0)

你总是可以使用REPLACE而不是拆分和重新加入,如果你想做的就是用'\ profilesNew \'替换'\ Program Files \'

UPDATE table SET column = REPLACE(column, '\Program Files\', '\profilesNew\');

答案 4 :(得分:0)

您是否只是想将“Program Files”更改为“ProfilesNew”?如果是这样,以下将执行此操作:

DECLARE @val VARCHAR(100);
SET @val = 'C:\Program Files\Internet Explorer\en-US';

SELECT REPLACE(@val, 'C:\Program Files', 'C:\ProfilesNew');

为您提供:C:\ProfilesNew\Internet Explorer\en-US

答案 5 :(得分:0)

declare @val as varchar(100)

set @val='C:\Program Files\Internet Explorer\en-US'

select @val

select replace(@val,Program Files',Program FilesNew')