Discussion Locked

Searching Similar Data in a Field

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need to search a field in a table for records with similar values. For example, an invoice number, "123", may also appear as "0123". Here is the query I wrote, which doesn't give me any results. I even created a record that looks like the example. Any suggestions.

SELECT *
FROM dbo.tbl_cds_paid
WHERE (inv_no LIKE '%' + rtrim([inv_no])) and inv_no <> inv_no

2 replies

by rwblaas

your where clause is incorrect. Your last predicate (and inv_no <> inv_no) will always be true and therefore your result set will always be a null set.

I'm not quite sure why you put the last predicate in, taking it out will give you the desired result. However you may need to add a wild card '%' at the end of the comparison if you wish to look for '123' in the complete strings, as '123' can be found anywhere in the middle of that string.

by frank

If you are searching for numeric values then you would have to use this wild card character _ . example would be _123 etc. I hope this will help you.
Take care.

This discussion is closed to new replies.