mardi 10 février 2015

selecting from either of two columns in SELECT

In SQL Server 2008R2 I have two tables, CONFIG_DATA and CNA_LIST:


CONFIG_DATA has two fields: ID [varchar(20)] and Address [int]:


01 141516

02 132132


CNA_LIST has three fields: Address [int], IP1 and IP2, both [varchar(20)]


141516 1.2.3.4 (null)


132132 (null) 2.3.4.5


These are field devices that have one or two IP addresses, in the IP1 or IP2 field (can be either, or both).


I would like to list the CONFIG_DATA table with IP1 from the CNA_LIST table, and if it's NULL, the IP2.


The Address field links the two tables.


this would work fine:



select a.ID, a.Address, b.IP1
from CONFIG_DATA a, CNA_LIST b where a.Address = b.Address


but if IP1 is null I need IP2, with this column listed as "IP":


I've tried IF(b.IP1 <> '')b.IP1 ELSE b.IP2 but the syntax checker complains.


My apologies - after 20 minutes, I still can't format this post to look right!


thanks for any help.


Aucun commentaire:

Enregistrer un commentaire