SQL Server determines whether duplicate values

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.