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

  • Home
  • SEARCH
  • 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 6148659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:19:07+00:00 2026-05-23T19:19:07+00:00

I have a table with 1,000,000+ records and I would like to find the

  • 0

I have a table with 1,000,000+ records and I would like to find the most common sub string that is at least 5 characters long.

If I have the following entries:

KDHFOUDHGOENWFIJ 1114H4363SDFHDHGFDG
GSDLGJSLJSKJDFSG 1114H20SDGDSSFHGSLD
SLSJDHLJKSSDJFKD 1114HJSDHFJKSDKFSGG

I would like to write in SQL a statement that selects 1114H as the most commmon sub string. How can I do this?

Notes:

  • The substring does not have to be in the same location.
  • The subtrings must be length 5
  • The maximum length of each record is 50 characters
  • 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-05-23T19:19:08+00:00Added an answer on May 23, 2026 at 7:19 pm

    There are no requirement to find the longest substring so every substring with length greater than 5 will always have a substring of 5 characters that is a tie for count. So we only have to check substrings of length 5.

    In the sample data there are three strings that occur three times. _1114H, _1114 and 1114H (_ is to show the location of a space ).

    In this solution master..spt_values is used in place of a numbers table.

    declare @T table
    (
      ID int identity,
      Data varchar(50)
    )
    
    insert into @T values
    ('KDHFOUDHGOENWFIJ 1114H4363SDFHDHGFDG'),
    ('GSDLGJSLJSKJDFSG 1114H20SDGDSSFHGSLD'),
    ('SLSJDHLJKSSDJFKD 1114HJSDHFJKSDKFSGG')
    
    select top 1 substring(T.Data, N.Number, 5) as Word
    from @T as T
      cross apply (select N.Number
                   from master..spt_values as N
                   where N.type = 'P' and
                         N.number between 1 and len(T.Data)-4) as N
    group by substring(T.Data, N.Number, 5)                      
    order by count(distinct id) desc
    

    Result:

    Word
    ------
     1114
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a table that looks like this: ProjectID TaskID Date 000 001
I have a data table with 600,000 records that is around 25 megabytes large.
I have several million records in a table (book/media data) and I would like
I have a cfinvoke which writes to a table like 200,000 records. Then I
i have a table with about 200,000 records. i want to add a field
I have a users table with 450,000 records. The table structure is as so:
I have quite a large table with 19 000 000 records, and I have
I have a SQL Server database with 500,000 records in table main . There
I have a SharePoint external list that points to a 100,000 record SQL table.
I have a rather large table with 150,000 + records. It has a lot

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.