1

I want to be able to take a users search input and find records that match in SQL.

I'm currently using Name like '%' + @SearchText + '%' ) which works fine if they enter in the text the correct way.

Example

If they search for "Jaws Revenge" or "Jaws 2 the revenge" or "revenge jaws" they won't get any results. I want it to return 1 result, id #2 from the table below.

Movies Table
---------------------
ID  Name
1   Jaws
2   Jaws 2: The revenge
3   Jaws 3-D
4   Rocky 5000

I've read about full text search but I don't know if that's worth it, the only option, or if there is a more simple solution.

6
  • 2
    I added the SQL Server tag based on the syntax. You should tag your questions with the database you are actually using. Commented Jul 30, 2017 at 13:23
  • @GordonLinoff Thanks for the tip! Commented Jul 30, 2017 at 13:24
  • 1
    As per your comment in Gordon's answer, you need FULLTEXT Index Commented Jul 30, 2017 at 13:27
  • @Pரதீப் Can you provide an example or link? Also, is that available on cloud hosting like AWS or Azure? Commented Jul 30, 2017 at 13:28
  • It is available in Azure learn.microsoft.com/en-us/sql/t-sql/statements/… Commented Jul 30, 2017 at 13:31

2 Answers 2

2

You can replace spaces with %:

Name like '%' + replace(@SearchText, ' ', '%') + '%' 
Sign up to request clarification or add additional context in comments.

1 Comment

This is getting closer but it doesn't work for searches like revenge jaws. I know the OP didn't state that case, sorry. I updated the op
0

You can use like '%' + replace(@SearchText, ' ', '%') + '%' in expration

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.