Nulls last in order by

  • I have the following select statement in a stored proc

    SELECT DISTINCT P.PlantNumber

    ,TSS.SpecimenTestDateTime

    ,TSH.SampleReferenceID

    ,TSS.SpecimenLetter

    ,TSH.TestSampleID

    ,TSS.TestSampleSpecimenID

    and an order by clause

    ORDER BY P.PlantNumber

    ,TSS.SpecimenTestDateTime

    ,TSH.TestSampleID

    ,TestSampleSpecimenID

    What I need to happen is that any PlantNumber that is Null should come last in the list as at the moment they are at the beginning. PlantNumber is a varchar field. It's probably simple but I've searched for it and can't fins anything useful

  • NULLS are first alphabetically. If you want them last, you need to sort Plantnumber descending.

    Or you can add a dedicated sort column: all rows which are not null have 1, and NULL rows have 999.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Thanks for the quick response, it's obvious when you think about it, I obviously wasn't thinking 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply