Groups | Blog | Home
all groups > sql server mseq > march 2004 >

sql server mseq : QRY question: If field1 is null then field2


Dan
3/24/2004 10:25:11 AM
Is there a way to create a column that will show the
value of field1 unless field1 is null in which case it
will show the value of field2? I do it all the time in
Access but can't seem to figure out if SQL Server can do
it as well. Any help or suggestions are geratly
appreciatd!

Here is how I do it in MS Access:
SELECT IIf([Field1] Is Null,[Field2],[Field1]) AS [Output]
Steve Kass
3/24/2004 1:35:43 PM
Dan,

In SQL Server, you can write

CASE WHEN Field1 IS NULL THEN Field2 ELSE Field1 END AS [Output]

Since this particular need comes up often, there is a shorthand form:

COALESCE(Field1, Field2) AS [Output]


Steve Kass
Drew University

[quoted text, click to view]
AddThis Social Bookmark Button