得到指定字串列表中,指定個數的字串

來源:互聯網
上載者:User

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_split]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_split]
GO

/*--得到字串列表指定位置的字元
 
 可以自訂字串列表的分隔字元
 如果取數位置超出的範圍,返回Null 字元串

--鄒建 2004.07(引用請保留此資訊)--*/

/*--調用樣本

 --測試資料
 declare @t table(FITEM varchar(100))
 insert @t select '100.120.10' 
 union all select '20.140.10'
 union all select '150.124.150.10'
 
 --查詢
 select fitem1=dbo.f_split(fitem,1,'.')
  ,fitem2=dbo.f_split(fitem,2,'.')
  ,fitem3=dbo.f_split(fitem,3,'.')
  ,fitem4=dbo.f_split(fitem,4,'.')
 from @t
--*/
create function f_split(
@s varchar(8000),  --字串列表
@pos int, --取數位置
@splitchar varchar(10) --分隔字元
)returns varchar(8000)
as
begin
 declare @i int,@ilen int

 select @i=charindex(@splitchar,@s),@ilen=len(@splitchar)
 while @i>0 and @pos>1
  select @s=substring(@s,@i+@ilen,8000)
   ,@i=charindex(@splitchar,@s)
   ,@pos=@pos-1
 return(case @pos when 1
   then case when @i>0 then left(@s,@i-1) else @s end
   else '' end)
end
go

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.