Skip to content
Advertisement

Search using LIKE operator with multiple dynamic values accepting both full and partial text match

Is there any way to do multiple term search in a column using like operator dynamically in SQL Server? Like below

For example: input values “goog; micro; amazon;” (input value should auto split by delimiter ‘;’ and check the text exist in the table) means that Search term ‘goog’ or ‘micros’ or ‘amazon’ from company column, if exists return.

Table – sample data:

Basically, The above query should return the results as like below,

Desired results:

Is it possible to achieve in SQL Server by splitting, then search in query? I look forward to an experts answer.

Advertisement

Answer

If you pass in a table valued parameter, you can join on that.

So for example

The one issue with this is that someone could write le; Mic and still get a result.

Strictly speaking, your table design is flawed, because you are storing multiple different items in the same column. You really should have this normalized into rows, so every Company is a separate row. Then your code would look like this:

You can simulate it by splitting your string

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement