Null Date Values and order by

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dean Mathews

    Null Date Values and order by

    How do I order a query by a date field ASC, but have any NULL values
    show up last? i.e.

    7/1/2003
    7/5/2003
    7/10/2003
    <NULL>
    <NULL>


    Any help will greatly be appreciated
  • Erland Sommarskog

    #2
    Re: Null Date Values and order by

    Dean Mathews (paradigmis@hot mail.com) writes:[color=blue]
    > How do I order a query by a date field ASC, but have any NULL values
    > show up last? i.e.
    >
    > 7/1/2003
    > 7/5/2003
    > 7/10/2003
    ><NULL>
    ><NULL>[/color]

    ORDER BY colaesce(date, '99991231')

    --
    Erland Sommarskog, SQL Server MVP, sommar@algonet. se

    Books Online for SQL Server SP3 at
    SQL Server 2025 redefines what's possible for enterprise data. With developer-first features and integration with analytics and AI models, SQL Server 2025 accelerates AI innovation using the data you already have.

    Comment

    • Andre Kuyt

      #3
      Re: Null Date Values and order by

      paradigmis@hotm ail.com (Dean Mathews) wrote in message news:<e32dbacc. 0307240715.5627 9933@posting.go ogle.com>...[color=blue]
      > How do I order a query by a date field ASC, but have any NULL values
      > show up last? i.e.
      >
      > 7/1/2003
      > 7/5/2003
      > 7/10/2003
      > <NULL>
      > <NULL>
      >
      >
      > Any help will greatly be appreciated[/color]

      simply use the following in the ORDER BY Clause
      select
      datefield
      from
      table
      ORDER BY
      ISNULL(datefiel d, '20550101')

      (or any other date far larger than the maximum of your resultset)

      Comment

      • Dean Mathews

        #4
        Re: Null Date Values and order by

        This works great, thanks for all your help

        Dean

        Comment

        Working...