Remove Duplicate Data From String

We can use below function to remove duplicate data from string .
 
  1. CREATE FUNCTION [dbo].[DistinctList]  
  2. (  
  3. @List VARCHAR(MAX),  
  4. @Delim CHAR  
  5. )  
  6. RETURNS  
  7. VARCHAR(MAX)  
  8. AS  
  9. BEGIN  
  10. DECLARE @Return_List [varchar](max);  
  11. DECLARE @Temp_Str [varchar](max);  
  12. DECLARE @Char_index int;  
  13. SET @List=@List+@Delim;  
  14. SET @Return_List='';  
  15. SET @Char_index=CHARINDEX(@Delim,@List,1);  
  16. WHILE @Char_index>0  
  17. BEGIN  
  18. SET @Temp_Str=SUBSTRING(@List,1,@Char_index-1);  
  19. SET @Return_List=@Return_List+@Temp_Str+@Delim;  
  20. SET @List=REPLACE(@List,@Temp_Str+@Delim,'');  
  21. SET @Char_index=CHARINDEX(@Delim,@List,1);  
  22. END  
  23. Return SUBSTRING(@Return_List,1 ,LEN(@Return_List)-1);  
  24. END   
In this function first parameter take the string  and second parameter the delimiter ,on the behalf of this delimiter we split the string and remove the duplicate data.
Example:
  1. DECLARE @String [varchar](max);  
  2. SET @String='10,11,12,10,11';  
  3. SELECT dbo.DistinctList(@String,','AS List;  
Output:

10,11,12