Re: QRY question: If field1 is null then field2

From: Steve Kass (skass_at_drew.edu)
Date: 03/24/04

  • Next message: Vishal Parkar: "Re: Query Confusion -- Please help"
    Date: Wed, 24 Mar 2004 13:35:43 -0500
    
    

    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

    Dan wrote:

    >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]
    >FROM [Table];
    >
    >


  • Next message: Vishal Parkar: "Re: Query Confusion -- Please help"

    Relevant Pages

    • Re: After RAISERROR executes next line.
      ... declare @x int ... Steve Kass ... Drew University ... >I am trying to raise an error in MS SQL Server 2000 using the lines ...
      (microsoft.public.sqlserver.server)
    • Re: Bit math in SQL
      ... Steve kass ... Drew University ... Dan Holmes wrote: ... > The promblem is i have an order that can be picked by multiple methods. ...
      (microsoft.public.sqlserver.programming)
    • Re: need help to create query
      ... I'm not sure, however, that SQL Server optimizes ALL queries as well as ... Steve Kass ... Drew University ...
      (microsoft.public.sqlserver.programming)
    • Re: System Administrator Implied Permissions
      ... >> Hi Dan, ... >> Now, given this, why does SQL Server ... However, these logins were not ... Other sysadmin role members have the ...
      (microsoft.public.sqlserver.security)
    • Re: System Administrator Implied Permissions
      ... member of the dbo group and you are automatically given ... Now, given this, why does SQL Server ... Much Thanks Dan. ...
      (microsoft.public.sqlserver.security)