Checking records with spaces
Posted on 2011-09-06
I am working with Microsoft SQL 2005. I have 2 tables, Games and Teams. Games has 2 columns visitingteam and hometeam. Teams has one column named teamname. I would like to select records in Games where visitingteam and hometeam are not in games. I have tried this:
SELECT * FROM Games
WHERE NOT EXISTS
((SELECT teamname FROM Teams WHERE teamname = Games.visitingteam)
(SELECT teamname FROM Teams WHERE teamname = Games.hometeam))
I think the issue may be with extra spaces in the tables. I tried RTRIM(LTRIM()) as well as several different uses of wildcards, but I could not get anything to work. When I did get a result from the query it returned the entire games table. I am not sure how to proceed.