日期:2014-05-17 浏览次数:20579 次
--創建函數
create function [dbo].[f_firstposition](@Str varchar(8000),@StrSep varchar(10),@AppPos int)
returns int
begin
declare @i int
declare @ii int
set @Str=rtrim(ltrim(@Str))
set @i=1
select @ii=charindex(@StrSep,@Str)
if @i=@AppPos
return @ii
else
while @AppPos>@i
begin
if charindex(@StrSep,right(@Str,len(@Str)-@ii))<>0
select @ii=charindex(@StrSep,right(@Str,len(@Str)-@ii))+@ii
else
set @ii=0
set @i=@i+1
end
return @ii
END
GO
--執行查詢
;WITH a1 (cstr) AS
(
SELECT 'shi yi gong'
)
SELECT REPLACE(cstr,' ','') cstr FROM a1
UNION ALL
SELECT STUFF(cstr,dbo.f_firstposition(cstr,' ',2),1,'') cstr FROM a1
UNION ALL
SELECT STUFF(cstr,dbo.f_firstposition(cstr,' ',2),1,'-') cstr FROM a1