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
 General SQL Server Forums
 New to SQL Server Programming
 new column

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2014-06-04 : 05:46:13
i have a list of data..how can i do it this way?

if tableB columnA exist then named it as newcolumnExist
if tableB columnA not exist then named it as newcolumnNotExist

i tried this but it only appear those data that exist for 'master'

select * from tableA join tableB on tableA.ID=tableB.ID
where tableB.columnA='Master'

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-06-04 : 08:48:45
Perhaps this?
select *,
CASE
WHEN tableB.columnA = 'Master' THEN 'newcolumnExist'
ELSE 'newcolumnNotExist'
END AS newColumnStatus
from tableA join tableB on tableA.ID=tableB.ID
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2014-06-04 : 22:24:58
but this query still will show those which exist 'master':

ID NewColumn
1 newcolumnExist
1 newcolumnNotExist
Go to Top of Page

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-06-05 : 01:22:00
May be this change will help you

SELECT *
,CASE WHEN TableB.columnA IS NOT NULL THEN 'newcolumnExist'
ELSE 'newcolumnNotExist'
END AS newColumnStatus
FROM TableA
LEFT JOIN TableB on TableA.ID=TableB.ID


---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page
   

- Advertisement -