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%'
BJones
  • 2,450
  • 2
  • 17
  • 25
  • Is there a better way to write what query? – BJones Jun 09 '16 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. – Sean Lange Jun 09 '16 at 17:51
  • Maybe you should start here? http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ – Sean Lange Jun 09 '16 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. – Krish Venkat Jun 09 '16 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%' ` – Alex Kudryashev Jun 09 '16 at 18:06

1 Answers1

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%'
Matt
  • 782
  • 4
  • 11
  • `Rutherford Rd` exists not only in `Jacksontown County` ;( – Alex Kudryashev Jun 09 '16 at 18:07
  • 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. – Krish Venkat Jun 09 '16 at 18:17
  • 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, – Krish Venkat Jun 09 '16 at 18:39
  • I think I got something that might work. I referred to http://stackoverflow.com/questions/29389293/sql-query-to-find-if-a-string-contains-part-of-the-value-in-column. Thanks all for your help. – Krish Venkat Jun 09 '16 at 18:45