sql字符串分组
create function f_myWord(@s varchar(50)) returns varchar(50) as begin declare @i int set @i=1 while @i<=3 begin set @s=SUBSTRING(@s,CHARINDEX(',',@s)+1,len(@s)) set @i=@i+1 end return @s end --执行的时候就 select dbo.f_myWord(列名) from 你的哪个表
-- ============================================= -- Author: 阿瑞 -- Create date: 2008-03-19 -- Description: split函数 -- Debug:select * from dbo.Fun_Split('ABC:BC:C:D:E',':') -- ============================================= CREATE FUNCTION [dbo].[Fun_Split] ( @SourceSql varchar(8000), @StrSeprate varchar(10) ) RETURNS @TEMP_Table TABLE (a varchar(100)) AS BEGIN DECLARE @i int SET @SourceSql=rtrim(ltrim(@SourceSql)) SET @i=charindex(@StrSeprate,@SourceSql) WHILE @i>=1 BEGIN INSERT @TEMP_Table VALUES(left(@SourceSql,@i-1)) SET @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i) SET @i=charindex(@StrSeprate,@SourceSql) END if @SourceSql<>'/' INSERT @TEMP_Table values(@SourceSql) RETURN END
调用:select * from dbo.Fun_Split('ABC:BC:C:D:E',':')