Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:33:51+00:00 2026-06-01T23:33:51+00:00

In T-SQL, how would you check if a string contains two or more similar

  • 0

In T-SQL, how would you check if a string contains two or more similar characters?

I have column contains mobile numbers of nvarchar which could be “5512111445”,”6612888445″ or hidden numbers like “5512zzz44x”

I have a search pattern entered by the user which could be “xx12yyy4zx” and i would like to return all the matched numbers to this pattern where x,y,z representing any number, but if it’s repeated it will representing the same number. the previous pattern for example should return all the listed numbers above.

xx are similar numbers like 55 or 66.. whereas xy are different numbers like 45 or 67..

How can this be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:33:53+00:00Added an answer on June 1, 2026 at 11:33 pm

    You could pivot each character in the mask and number into columns, and then group on mask alone followed by mask + number. In this method 5512111445 and 6612888445 do not match mask xx12yyy4yz because the y in mask does not map to a unique digit. However, mobile numbers 5512111415 and 6612888485 do match mask xx12yyy4yz, as does mobile number 5512zzz44x.

    --declare @mobileNums varchar(10)='5512111445'; --no match because @mask y maps to different values
    --declare @mobileNums varchar(10)='6612888445'; --no match because @mask y maps to different values
    --declare @mobileNums varchar(10)='5512111415'; --no match because @mask x should not equal @mask z
    --declare @mobileNums varchar(10)='6612888485'; --matches
    --declare @mobileNums varchar(10)='8812888485'; --no match because @mask x should not equal @mask y
    --declare @mobileNums varchar(10)='5512zzz44x'; --matches because z and x are both hidden and different
    --declare @mask varchar(10)='xx12yyy4yz';
    
    declare @mobileNums varchar(10)='3211zyy'; -- no match because @mask y <> @mask z, but @mobileNums y = y
    declare @mask varchar(10)='3211yxz';
    
    declare @t table(n char, m char);
    declare @i int=1;
    
    while @i<=LEN(@mobileNums) begin
        insert into @t values (SUBSTRING(@mobileNums,@i,1), SUBSTRING(@mask,@i,1));
        set @i+=1;
    end
    
    if exists(
        -----------------------------------------------------------------------------
        -- Group by m
        select
        m, c=count(m)
        from @t
        where ISNUMERIC(n)=0 and ISNUMERIC(m)=0
        group by m
        except
        select
        m, c=count(m+n)
        from @t
        where ISNUMERIC(n)=0 and ISNUMERIC(m)=0
        group by m,n
    
        union
    
        select
        m, c=count(m)
        from @t
        where ISNUMERIC(n)=0 and ISNUMERIC(m)=1
        group by m
        except
        select
        m, c=count(m+n)
        from @t
        where ISNUMERIC(n)=0 and ISNUMERIC(m)=1
        group by m,n
    
        union
    
        select
        m, c=count(m)
        from @t
        where ISNUMERIC(n)=1 and ISNUMERIC(m)=0
        group by m
        except
        select
        m, c=count(m+n)
        from @t
        where ISNUMERIC(n)=1 and ISNUMERIC(m)=0
        group by m,n
    
        union
    
        select
        m, c=count(m)
        from @t
        where ISNUMERIC(n)=1 and ISNUMERIC(m)=1
        group by m
        except
        select
        m, c=count(m+n)
        from @t
        where ISNUMERIC(n)=1 and ISNUMERIC(m)=1
        group by m,n
    
        union
    
        -----------------------------------------------------------------------------
        -- Group by n
    
        -- Add a rule that no numeric @mobileNums digit can correspond to more than one alpha @mask character
        select
        n, c=count(m)
        from @t
        where ISNUMERIC(n)=1 and ISNUMERIC(m)=0
        group by n
        except
        select
        n, c=count(m+n)
        from @t
        where ISNUMERIC(n)=1 and ISNUMERIC(m)=0
        group by m,n 
    
        union
    
        -- For GROUP BY n, include the three remaining combinations of ISNUMERIC(n) and ISNUMERIC(m)
        select
        n, c=count(m)
        from @t
        where ISNUMERIC(n)=0 and ISNUMERIC(m)=0
        group by n
        except
        select
        n, c=count(m+n)
        from @t
        where ISNUMERIC(n)=0 and ISNUMERIC(m)=0
        group by m,n    
    
        union
    
        select
        n, c=count(m)
        from @t
        where ISNUMERIC(n)=0 and ISNUMERIC(m)=1
        group by n
        except
        select
        n, c=count(m+n)
        from @t
        where ISNUMERIC(n)=0 and ISNUMERIC(m)=1
        group by m,n 
    
    )
    select patMatch='False'
    else 
    select patMatch='True';
    

    EDIT – Add a rule that no numeric @mobileNums digit can correspond to more than one alpha @mask character

    EDIT – For GROUP BY n, include the three remaining combinations of ISNUMERIC(n) and ISNUMERIC(m)

    EDIT – removing the eighth UNION

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server 2005 table that has a string column in which
How would you check in php that a string is a valid compatible column
In T-SQL, how would you check if a string doesn't contain another string? I
How would I reverse contains in a Linq-to-SQL query so that I can check
I have a String variable abc . Now I would like to check whether
Some help with some T-SQL would be most appreciated. I have the following four
I have C# application that must store some information into MS SQL that would
I wrote the following part which is a Java method with two STRING arguments
I have two entities ( Dataset and Item ), where the first one contains
I'd like to check a connection string on a SQL 2008 server. Problem is,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.