0

I have a table called Employee with a field called address. I need to write a select that will retrieve all those rows from the table that whose address has a string or part of the string being passed.

So for instance, the address being passed to the query is "7745 Rutherford Rd, Jacksontown County". But the address field has records with only Jacksontown County. Not the entire one mentioned above and I need these rows to be retrieved too.

I tried using the LIKE operator but that will not work I think as the string being passed is longer than what we have in the database. Is there a better way to write this query?

Query:

SELECT * 
FROM Employee 
WHERE address LIKE '%7745 Rutherford Rd,Jacksontown County%'
5
  • Is there a better way to write what query? Commented Jun 9, 2016 at 17:51
  • Is there a better way than what? You didn't provide the query you are using now. More importantly is your question doesn't have enough context to make a lot of sense. Commented Jun 9, 2016 at 17:51
  • Maybe you should start here? spaghettidba.com/2015/04/24/… Commented Jun 9, 2016 at 17:52
  • Sorry about that.The query I tried was SELECT * FROM Employee WHERE address LIKE '%7745 Rutherford Rd,Jacksontown County%'. This query does not work.As I was trying to say above,the address field will have Jacksontown County only.I want those records to be retrieved too. Commented Jun 9, 2016 at 17:59
  • 1
    Add weaker conditions SELECT * FROM Employee WHERE address LIKE '%7745 Rutherford Rd%Jacksontown County%' or address LIKE '%Rutherford Rd%Jacksontown County%' or address LIKE '%Jacksontown County%' Commented Jun 9, 2016 at 18:06

1 Answer 1

0

You should pass each part of the string separately.

SELECT * 
FROM Employee 
WHERE address LIKE '%7745%' 
    or address LIKE '%Rutherford Rd%'
    or address LIKE '%Jacksontown County%'
Sign up to request clarification or add additional context in comments.

4 Comments

Rutherford Rd exists not only in Jacksontown County ;(
what I am looking for is something else.Basically what I need is to retrieve all those rows from the table whose address field value matches part or the whole String being passed. The strings being passed are dynamically generated. Unfortunately in this forum since I cannot show the actual query that I am using,I was trying to come up with an example.Hope this is clearer.
another example.If I have a row in the table as TEST and if my searchString has 'THIS IS A TEST FOR TODAY',then that row needs to be retrieved.Not sure how to write a query for this,
I think I got something that might work. I referred to stackoverflow.com/questions/29389293/…. Thanks all for your help.

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.