Sam OZ
asked on
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
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
Here's a somewhat brute force approach, not too elegant but should get the job done.
»bp
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%';
»bp
ASKER
My apologies. I did not make it clear - I need only repetition of "." captured . Not any other characters
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...
Or if checking a specific character then much easier...
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not sure I understand, I read:
»bp
My apologies. I did not make it clear - I need only repetition of "." captured . Not any other charactersAnd interpreted that to mean they were looking for multiple contiguous periods, did I miss something?
»bp
Waiting for a better explanation of the problem.
@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 :)
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 :)
Agreed, given the "desired results" in the original question, I will rally around:
»bp
SELECT *
FROM Table1
WHERE VersionNo LIKE '%.%.%';
»bp
»bp