0

If I have a value something like '10,10,20,30,40,20' in a field of table, then I want to make it as '10,20,30,40'

Is there any sql function to do such thing?

Thanks Sudhakar

4
  • 1
    Don't store multiple values in a single column. Instead store them in separate records in a related table. Then it's easy with DISTINCT. Yu also have more safety(correct data type) and much better performance. Commented Oct 13, 2017 at 12:49
  • Use the split function, and then select distinct on the results ans put into a string using the coalesce trick Commented Oct 13, 2017 at 12:52
  • Yes myfriend, I know it. But the problem is it is age old system and client wants like that only. He is not ready to change the design as he afraid it may impact other functions of the legacy system So I am forced to use the design the way it is now :( Commented Oct 13, 2017 at 12:54
  • 2
    @RegBes STRING_SPLIT is only working with SQL2016+. Commented Oct 13, 2017 at 12:57

3 Answers 3

2

using Jeff's DelimitedSplit8K from http://www.sqlservercentral.com/articles/Tally+Table/72993/

declare @value      varchar(100) = '10,10,20,30,40,20',
    @new_value  varchar(100)

select  @new_value = isnull(@new_value + ',', '')  + Item
from    DelimitedSplit8K(@value, ',')
group by Item
order by Item

select  @new_value
Sign up to request clarification or add additional context in comments.

Comments

1

Did this long ago. This might need some modifications. But it generates output.

Try :

DECLARE @Data_String AS VARCHAR(1000), @Result as varchar(1000)=''
SET @Data_String = '10,10,20,30,40,20'
SET @Data_String = REPLACE(@Data_String,'|',',')

select @Data_String;

SELECT @Result=@Result+col+',' from(
    SELECT DISTINCT t.c.value('.','varchar(100)') col from(
        SELECT cast('<A>'+replace(@Data_String,',','</A><A>')+'</A>' as     xml)col1)data 
    cross apply col1.nodes('/A') as t(c))Data
SELECT LEFT(@Result,LEN(@Result)-1)

Comments

0

believing it stores integer number you can get them with creating a function first you need to split the values then have to use a distinct function as below

1st create a function like

CREATE FUNCTION [dbo].[idpGetSplitedString]
(
    @String varchar(8000), 
    @Delimiter char(1)
)
RETURNS 
@temptable TABLE 
(
    items varchar(8000)
)
AS
BEGIN
    declare @idx int        
    declare @slice varchar(8000)        

    select @idx = 1        
        if len(@String)<1 or @String is null  return        

    while @idx!= 0        
    begin        
        set @idx = charindex(@Delimiter,@String)        
        if @idx!=0        
            set @slice = left(@String,@idx - 1)        
        else        
            set @slice = @String        

        if(len(@slice)>0)   
            insert into @temptable(Items) values(rtrim(ltrim(@slice)))        

        set @String = right(@String,len(@String) - @idx)        
        if len(@String) = 0 break        
    end  

    RETURN 
END

then call the function like

select [dbo].idpGetSplitedString as Values

3 Comments

You need to look around and find a better splitter. The one from Jeff Moden linked above is a great example. Here are many other much better alternatives. sqlperformance.com/2012/07/t-sql-queries/split-strings Looping is not a good for this.
any for that ?? cos this one does its job
Sure it will do the split. But it does it super slow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.