Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How to check if field value is in another table

Author  Topic 

capella07
Starting Member

46 Posts

Posted - 2007-06-26 : 10:43:51
Hi all,

I need to check if a value in a field of one table is found in the field of another table, and if so, display another field in the second table, otherwise display a field in the first table.

For example, Table 1 has the fields NameUID(key), Name, EmpAddress, and WorkPlaceID. Table 2 has the fields BuildingUID(key), BldgAddress, and WorkPlaceID.

Not all records in Table 1 will have a WorkPlaceID that is in Table 2.

If the WorkPlaceID in Table 1 is found in the WorkPlaceID field in Table 2 then display the BldgAddress, otherwise display the EmpAddress.

I hope that makes sense.

I'm not sure how to do that in SQL syntax. Would that be some kind of Join, or a Case, or would/could it be done some other way?

Thanks for anyone's help!

=====================================
f u cn rd ths, u cn gt a gd jb n prgrmng

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-26 : 10:47:26
You will need LEFT JOIN:

Select Coalesce(t2.BldgAddress, t1.EmpAddress) as Address
From Table1 t1 LEFT JOIN Table2 t2
on t1.WorkPlaceID = t2.WorkPlaceID


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -