Code set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Author: <MIN> -- Create date: <2007-12-12> -- Description: <分解字符串> -- ============================================= ---执行 -- SELECT * from dbo.Split_String ('a,b,c,d,we3r',',') ALTERFUNCTION[dbo].[Split_String] ( @split_stringvarchar(max), --要进行分解的字符串 @tag_stringvarchar(10) --分解标志 ) RETURNS @split_tableTABLE ( split_value varchar(200) ) AS BEGIN declare@temp_stringvarchar(max) declare@start_indexint declare@end_indexint
while1=1 begin set@start_index=0 select@end_index=CharIndex(@tag_string,@split_string,@start_index) if@end_index<>0 begin set@temp_string=Substring(@split_string,@start_index,@end_index) if@temp_stringisnotnulland@temp_string<>'' insertinto@split_table(split_value) values(@temp_string) set@start_index=@end_index+1 set@split_string=Substring(@split_string,@start_index,len(@split_string)) end else begin if@split_stringisnotnulland@split_string<>'' insertinto@split_table(split_value) values(@split_string) break end end