Check whether the SQL server has repeated values. Check whether the SQL server has repeated values.

Source: Internet
Author: User

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



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.