2

Is it possible to check if a specific substring which is in SQL Server column, is contained in a user provided string?

Example :

SELECT * FROM Table WHERE 'random words to check, which are in a string' CONTAINS Column

From my understanding, CONTAINS can't do such kind of search.

EDIT :

I have a fully indexed text and would like to search (by the fastest method) if a string provided by me contains words that are present in a column.

1
  • Be aware that you need a Full Text Index before you can use CONTAINS. Commented Aug 18, 2016 at 10:23

3 Answers 3

4

You can use LIKE:

SELECT * FROM YourTable t
WHERE 'random words ....' LIKE '%' + t.column + '%'

Or

SELECT * FROM YourTable t
WHERE t.column LIKE '%random words ....%' 

Depends what did you mean, first one select the records that the column has a part of the provided string. The second one is the opposite.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm looking for the first one, just wanted the fastest method. From what I read, LIKE operator isn't the fastest since it can't benefit from indexed columns.
I'm not having any luck getting results from the first form in either MySQL or sqlite. Is this a SQL Server–specific feature?
Probably the ‘+’ is the issue. Use Concat instead @davidmoles
@sagi Aha. LIKE CONCAT('%', t.column, '%') does work in MySQL. SQLite doesn't support CONCAT(), but it works with the || operator: LIKE '%' || t.column || '%'. I'm surprised neither threw an error with +, just returned an empty result — who knows what they think they're doing.
2

Just use the LIKE syntax together with % around the string you are looking for:

SELECT
   *
FROM
   table
WHERE
   Column LIKE '%some random string%'

This will return all rows in the table table in which the column Column contains the text "some random string".

Comments

0

1) If you want to get data starting with some letter you can use % this operator like this in your where clause

WHERE
Column LIKE "%some random string"

2) If you want to get data contains any letter you can use

WHERE
Column LIKE "%some random string%"

3)if you want to get data ending with some letter you can use

WHERE
Column LIKE "some random string%"

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.