例如 我查出一串字符 结果如下 
'APR-11,APR12,APR06,APR-11,APR12,APR06'
如何去除重复的 只保留一个

解决方案 »

  1.   

    没有什么函数吗  对XML也不太懂 
      

  2.   

    sql 中没有相应的函数,需要自己去写
      

  3.   

    declare @ret varchar(8000)
    select @ret = 'APR-11,APR12,APR06,APR-11,APR12,APR06'select distinct name=substring(@ret,number,charindex(',',@ret+',',number)-number)
    from master..spt_values
    where number<=len(@ret) and type='P' 
    and substring(','+@ret,number,1)=','
    --------------------------
    APR06
    APR-11
    APR12
      

  4.   


    create function GetDistinct(@str varchar(1000))
    returns varchar(1000)
    as
    begin
    declare @temp varchar(1000)
    while(charindex(','+substring(@str,1,charindex(',',@str)-1)+',',','+isnull(@temp,'')+',')=0)
    begin
    set @temp=isnull(@temp+',','')+substring(@str,1,charindex(',',@str)-1)
    set @str=substring(@str,charindex(',',@str)+1,len(@str))
    end
    return @temp
    end
    goselect dbo.getdistinct('APR-11,APR12,APR06,APR-11,APR12,APR06')
    --结果:
    --------------------------
    APR-11,APR12,APR06
      

  5.   

    declare @ret varchar(8000)
    select @ret = 'APR-11,APR12,APR06,APR-11,APR12,APR06'select @ret = 'select '''+replace(@ret,',',''' union select ''')+''''exec(@ret)
    ----------------------
    APR06
    APR-11
    APR12
      

  6.   

    --直接点的
    declare @str varchar(8000)
    declare @ret varchar(8000),@return varchar(8000)select @str = 'APR-11,APR12,APR06,APR-11,APR12,APR06'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 select @return
    ---------------------------
    APR-11,APR12,APR06