I am new in SQL Queries..
I have 2 database DB1 & DB2.
DB1 Contains the table A which has Name,Address & Code columns & DB2 contains the table B which has column Name1,Address1.
if the Code is not null then i want to select Name,Address from the tableA else name1 & address1 should come from tableB.
Please Help me on this.
select top 1 a.name 'MyName', a.address 'MyAddress' from DB1.table1 a where a.code <> null Assuming your tableA and TableB have primary key as "Name" select case when a.Code is not null then a.Name else b.Name end as Name ,case when a.Code is not null then a.Address else b.Address end as Address from DB1..tableA a left join DB1..tableB b on a.Name=b.nameOR
select case when isnull(a.Code,'')<>'' then a.Name else b.Name end as Name ,case when isnull(a.Code,'')<>'' then a.Address else b.Address end as Address from DB1..tableA a left join DB1..tableB b on a.Name=b.name





