sql 逗号分隔的字符串去重函数

sqlserver  函数:

 

Create function [dbo].[GetDistinct](@str varchar(1000))
returns varchar(1000)
as
BEGIN
   
        declare @ret varchar(8000),@return varchar(8000)
        select @str = @str+','
        
        while charindex(',',@str) > 0
        begin
            select @ret = substring(@str,1,charindex(',',@str)-1)
             
            select @return = isnull(@return+',','')+@ret
        
            select @str = replace(@str,@ret+',','')
        end
    RETURN @return
END

 

Mysql 函数:

 

CREATE  FUNCTION GetDistinct(str varchar(1000)) RETURNS varchar(1000)  
BEGIN 
    declare ret varchar(1000);
    declare returnstr varchar(1000);
    set str =concat( str,','); 
    while locate(',',str) > 0
        do
            set ret = substring(str,1,locate(',',str)-1),
              returnstr=concat( returnstr,','),
              returnstr = concat(ifnull( returnstr,''),ret),
              str = replace(str,concat( ret,','),''); 
    end while; 
    RETURN returnstr;
end

 

posted @ 2020-10-10 10:40  木子易  Views(1891)  Comments(1Edit  收藏  举报