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 3400434
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:53:32+00:00 2026-05-18T04:53:32+00:00

trying to do a small stored procedure without needing to add freetext indexing just

  • 0

trying to do a small stored procedure without needing to add freetext indexing just for this (SQL Server 2008)

Basically, I want to find all records where a certain field contains all the words from a parameter.

So if in the field I have “This is a test field”, and the parameter to my SP would be “this test field” it would return it, as it would if the parameter was “field this test”.

The table is very small (4000) record and load will be low, so efficiency is not a big deal. Right now the only solution i can think of is to split both strings with table valued function and go from there.

Any simpler idea?

Thanks!

  • 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-18T04:53:33+00:00Added an answer on May 18, 2026 at 4:53 am

    Here is a solution using recursive CTEs. This actually uses two separate recursions. The first one splits the strings into tokens and the second one recursively filters the records using each token.

    declare     
        @searchString varchar(max),
        @delimiter char;
    
    select 
    @searchString  = 'This is a test field'
    ,@delimiter = ' '
    
    declare @tokens table(pos int, string varchar(max))
    
     ;WITH Tokens(pos, start, stop) AS (
          SELECT 1, 1, CONVERT(int, CHARINDEX(@delimiter, @searchString))
          UNION ALL
          SELECT pos + 1, stop + 1, CONVERT(int, CHARINDEX(@delimiter, @searchString, stop + 1))
          FROM Tokens
          WHERE stop > 0
        )
        INSERT INTO @tokens
        SELECT pos,
          SUBSTRING(@searchString, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS string
        FROM Tokens
        OPTION (MAXRECURSION 25000) ;
    
    ;with filter(ind, myfield) as (
        select  1,myfield from mytable where myfield like '%'+(select string from @tokens where pos = 1)+'%'    
        union all
        select  ind + 1, myfield from filter where myfield like '%'+(select string from @tokens where pos = ind + 1)+'%'    
        )
    
        select * from filter where ind = (select COUNT(1) from @tokens)
    

    This took me about 15 seconds to search a table of 10k records for the search string ‘this is a test field’.. (the more words in the string, the longer it takes.. )

    Edit
    If you want a fuzzy search i.e return closely matching results even if there wasnt an exact match, you could modify the last line in the query to be –
    select * from (select max(ind) as ind, myfield from filter group by myfield) t order by ind desc

    ‘ind’ would give you the number of words from the search string found in myfield.

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

Sidebar

Related Questions

IBM Informix Dynamic Server Version 11.50.FC6 I was working on a small stored procedure
Trying to use Linq to SQL for a small project I'm working on at
I'm currently writing a small app that takes data provided by an SQL stored
I'm trying to convert the permanent tables used in a stored procedure to global
So I'm developing a small project with Cocos2Dx but I'm trying to add Bluetooth
I am trying to make a gridview sortable which uses a stored procedure as
I have a somewhat small database in SQL Server Express 2005 that I really
I am trying to write a small library which will use DirectShow. This library
Hi I m trying to execute small code in python , it is giving
Begginer's question. I'm trying to write small piece of code in Visual Studio (VB.net)

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.