T-SQL code(backup):
Create Function fn_slip_str( @InStr nvarchar(2000) , @s_char nvarchar(1) ) Returns @tb Table ( sno int , data nvarchar(100) ) As Begin /*依據傳入字元進行字串分割,回傳Table*/ /* Declare @InStr nvarchar(2000) , @s_char nvarchar(1); Set @InStr = '字串一,字串2,字串3,字串4'; Set @s_char = ',';*/ Set @InStr = @s_char + @InStr + @s_char; Declare @p1 Int , @p2 Int , @data nvarchar(100) , @sno int; Set @p1 = -1 ; Set @p2 = -1 ; Set @data = '' ; Set @sno = 0; While ( 0 Not In (@P1,@P2) ) Begin Set @p1 = CharIndex(@s_char,@InStr,@p1+1); Set @p2 = CharIndex(@s_char,@InStr,@p1+1); If ( 0 In (@p1,@p2) ) Break; Set @data = SubString(@InStr,@p1+1,@p2-@p1-1); if ( @data <> '' ) Begin Set @sno = @sno +1; Insert Into @tb ( sno , data ) Values ( @sno , @data ) End End Return End
執行結果:
Select * From dbo.fn_slip_str('字串一,字串2,字串3,字串4',',');
下段程式原始來源請參考下列網址(如有侵權請告知):
http://www.dotblogs.com.tw/rachen/archive/2008/05/23/4110.aspx
沒有留言:
張貼留言