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
 combine columns in select

Author  Topic 

Mauricio Moreno
Starting Member

18 Posts

Posted - 2007-08-20 : 18:31:50
I would appreciate any help with my following problem... lets say
i have...

select A.firstname + '' + B.lastname as fullname, 'Their Home is ' + A.City + ' ' + (select top 1 C.State from States C where C.City = A.City) as Location
from tableA A, TableB B
Where A.id = b.id

This is not the actual statement but follows the same kinda logic... the problem that i get is that some of the rows in both my fullname column and in my location column show up as null... how would i fix it so for instance even if the state is missing it would still show: their home is LA or if just the last name is available it would show the lastname?

Thank you

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-08-20 : 19:52:07
Use ISNULL or COALESCE for each column.


select COALESCE(A.firstname, '') + COALESCE(B.lastname , '')...

etc


Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

Mauricio Moreno
Starting Member

18 Posts

Posted - 2007-08-20 : 20:19:34
Oh Thank you!!!
Go to Top of Page
   

- Advertisement -