Sql to get characters repeat

Hi ,
  In Sql server 2016 , I need the Sql query check for a table field  if a character repeats more than once in a given value
    For example, in the following sample rows for VersionNo
     1.1.1
     1.1
      2
     2.1.1.2
     2.3
     As result of query , I should get
       1.1.1
        2.1.1.2

    Thanks
Sam OZAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Why wasn't "1.1" also selected as a valid result, it has two repeating "1"s?


»bp
1
Bill PrewCommented:
Here's a somewhat brute force approach, not too elegant but should get the job done.

SELECT * 
  FROM Table1
 WHERE REPLACE(VersionNo, '.', '') LIKE '%00%'
    OR REPLACE(VersionNo, '.', '') LIKE '%11%'
    OR REPLACE(VersionNo, '.', '') LIKE '%22%'
    OR REPLACE(VersionNo, '.', '') LIKE '%33%'
    OR REPLACE(VersionNo, '.', '') LIKE '%44%'
    OR REPLACE(VersionNo, '.', '') LIKE '%55%'
    OR REPLACE(VersionNo, '.', '') LIKE '%66%'
    OR REPLACE(VersionNo, '.', '') LIKE '%77%'
    OR REPLACE(VersionNo, '.', '') LIKE '%88%'
    OR REPLACE(VersionNo, '.', '') LIKE '%99%';

Open in new window


»bp
1
Sam OZAuthor Commented:
My apologies. I did not make it clear -   I need only  repetition of  "." captured . Not any other characters
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Mark WillsTopic AdvisorCommented:
Are they only ever numerals ?
Do you know what character to check for ?
Do you know what shouldnt be checked for ?

We could do something generic for any character...
WITH CTE_Nums AS
( SELECT number 
  FROM master.dbo.spt_values WHERE type = 'P'
),CTE_Strings AS
( SELECT S.YourColumn, IIF (SUBSTRING(S.YourColumn,n.Number,1) not in (' ','.'), CHARINDEX(SUBSTRING(S.YourColumn,n.Number,1),S.YourColumn,n.Number+1),0) recurs   
  FROM #Your_Table S
  JOIN CTE_Nums n ON n.Number > 1 and n.Number < LEN(S.YourColumn) 
) SELECT DISTINCT YourColumn
  FROM CTE_Strings where recurs > 0 

Open in new window


Or if checking a specific character then much easier...
SELECT YourColumn,(LEN(YourColumn) - LEN(REPLACE(YourColumn, '.', ''))) CountChars
FROM #Your_Table
WHERE (LEN(YourColumn) - LEN(REPLACE(YourColumn, '.', ''))) > 0

Open in new window

0
Bill PrewCommented:
Okay, then try.

SELECT * 
  FROM Table1
 WHERE VersionNo LIKE '%..%';

Open in new window


»bp
0
Mark WillsTopic AdvisorCommented:
Bill, that would need to be '%.%.%'
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
Not sure I understand, I read:
My apologies. I did not make it clear -   I need only  repetition of  "." captured . Not any other characters
And interpreted that to mean they were looking for multiple contiguous periods, did I miss something?


»bp
1
Vitor MontalvãoMSSQL Senior EngineerCommented:
Waiting for a better explanation of the problem.
0
Mark WillsTopic AdvisorCommented:
@bill, my interpretation was "How many times does a fullstop character (ie '.') appear in a string, and show me the string when more than 1"

Hence SELECT YourColumn FROM YourTable WHERE YourColumn LIKE '%.%.%'

Will give the results of 1.1.1 and 2.1.1.2  (having 2 and 3 dots respectively)

Although, I did really like my generic "any character" scan (back in #a42428634 ), I do think the wildcard search is the easiest to understand and do....

That assumes of course that we now understand the requirement correctly :) and given Vitor's comment above, there is some doubt.

@Vitor, Congrats on being a finalist for 2017 MSSQLTips Author of the Year. Well done :)
1
Bill PrewCommented:
Agreed, given the "desired results" in the original question, I will rally around:

SELECT * 
  FROM Table1
 WHERE VersionNo LIKE '%.%.%';

Open in new window


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.