|  | Posted by Erland Sommarskog on 05/27/05 00:57 
robert lassiter (rlassiter@shaw.ca) writes:> I have two tables in the same SQL database. Both have a similar numeric
 > field.  One field has the same number as the other field but is prefixed
 > with various letters. I know how to use LIKE but won't know the partial
 > string I am looking for.  I am trying to use LIKE '%' + Field A or
 > something that will do this.
 >
 > Eg.
 >
 > Table 1 Field A is 'A12345"
 > Table 2 Field B is '12345"
 >
 > I want to find every record in Table 1 Field A that contains the exact
 > Field B data.
 
 Assuming the the numbers are always at the end, this could do:
 
 SELECT * FROM tbl1 a
 WHERE  EXISTS (SELECT *
 FROM   tbl2 b
 WHERE  charindex(b.fieldb, a.fielda) =
 len(a.fielda) - len(b.fieldb) + 1)
 
 Note that this is untested.
 
 
 
 --
 Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
 
 Books Online for SQL Server SP3 at
 http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
  Navigation: [Reply to this message] |