A string of "1;2;1;1;1".
Excuse me:
How to implement the following features in SQL Server:
1. Determine if there are duplicate numbers in the string
If there are duplicate numbers, an error message is returned
If there are no duplicate numbers, the return is correct.
Idea: Splitting a string, depositing a substring into a temporary table, and grouping judgment on a temporary table
Create function repeatstring (@input varchar (8000), @separator varchar) returns INTAS begin Declare @temp Table (part varchar) declare @i int, @result int set @input =rtrim (LTrim (@input)) set @i=charindex (@ Separator, @input) while @i>=1 begin Inserts @temp values (left (@input, @i-1)) set @input =substring (@input, @i+1,len (@input) [email protected]) Set @i=charindex (@separator, @input) end if exists (select Part,count (*) from @temp GROUP by part has count (*) >1) Set @result = 1--there is duplicate else set @result = 0--There is no duplicate return @resultendgo
--Test
Select dbo. Repeatstring (' 1,1,2,3,1 ', ', ') --1select dbo. Repeatstring (' 1,2,3,4,5 ', ', ') --0
SQL Server determines whether duplicate values