Check whether the SQL server has repeated values. Check whether the SQL server has repeated values.
A string is "1; 2; 1; 1; 1 ".
Excuse me:
How to implement the following functions in SQL SERVER:
1. check whether there are repeated numbers in the string.
If a number already exists, an error message is returned.
If no duplicate number exists, the returned result is correct.
Train of Thought: Split the string, store the substring into a temporary table, and perform grouping and judgment on the temporary table
Create function RepeatString (@ input varchar (8000), @ separator varchar (10) returns intas begin declare @ temp table (part varchar (100) declare @ I int, @ result int set @ input = rtrim (ltrim (@ input) set @ I = charindex (@ separator, @ input) while @ I> = 1 begin insert @ temp values (left (@ input, @ i-1) set @ input = substring (@ input, @ I + 1, len (@ input) -@ I) set @ I = charindex (@ separator, @ input) end if exists (select part, count (*) from @ temp group by part having count (*)> 1) set @ result = 1 -- repeated else set @ result = 0 -- repeated return @ resultendgo does not exist
-- Test
Select dbo. RepeatString ('1, 1, 2, 3, 1 ',') -- 1 select dbo. RepeatString ('1, 2, 3, 4, 5 ',', ') -- 0