Friday, March 23, 2012

How to test if one col is "like" another?

We have all done the following:
SELECT COUNT(*) FROM TAB1
WHERE TAB1.COL1 LIKE '%mystring%'
I would like to replace the mystring with another column in the same table.
In this case the StreetName is both a separate column AND contained in the
StreetAddress column.
EX. StreetAddress = '123 Main Street, APT 10'
Streetname = 'Main Street'
Here is a non-functioning logical sample:
Select count(*) From Tab1 T1
Where T1.StreetAddress Like %T1.StreetName%
How can I check if one column is contained somewhere in another?
Thanks,
Michael...
WHERE streetaddress LIKE '%'+streetname+'%'
or
...
WHERE CHARINDEX(streetname,streetaddress)>0
David Portas
SQL Server MVP
--|||It is difficult to believe that the answer is so straight-forward!
Thanks!
"David Portas" wrote:

> ...
> WHERE streetaddress LIKE '%'+streetname+'%'
> or
> ...
> WHERE CHARINDEX(streetname,streetaddress)>0
> --
> David Portas
> SQL Server MVP
> --
>

No comments:

Post a Comment