Why does this SQL order null values last?

Chris Halcrow

This will apparently put null values for myDate at the bottom of the result set. What's the logic behind how this is being executed?

SELECT * FROM myTable
WHERE ...
ORDER BY CASE WHEN myDate IS NULL THEN 1 ELSE 0 END, myDate;
Gordon Linoff

This is your order by:

ORDER BY (CASE WHEN myDate IS NULL THEN 1 ELSE 0 END),
          myDate

The first expression for the order by says "Give the NULL values a value of 1 (for the sort) and non-NULL values a value of 0". Well, you are sorting in ascending order, so the NULL values go last.

If you want them first, use desc:

ORDER BY (CASE WHEN myDate IS NULL THEN 1 ELSE 0 END) DESC,
          myDate

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does the order of alternatives matter in regex?

분류에서Dev

Why does the order of applying advice matter?

분류에서Dev

why does the order of variable declaring matter?

분류에서Dev

How does work SQL with order by 0?

분류에서Dev

Why does SSIS lookup return null value?

분류에서Dev

Why does this MySQL stored function return null?

분류에서Dev

Replacing null values in dynamic pivot sql query

분류에서Dev

Not able to allow null values in my sql script

분류에서Dev

Why does `iperf` report huge values for bandwidth?

분류에서Dev

Does Play provide any guarantees for queryString values order?

분류에서Dev

Why does the malloc order matter when a struct is sufficiently sized?

분류에서Dev

Why MySQL does not return rows in same order as appeared in IN clause?

분류에서Dev

Why does Order matter in Kwarg parameters in MagicMock asserts?

분류에서Dev

Does the order of JOIN vs WHERE in SQL affect performance?

분류에서Dev

Why does my linq to sql query fail?

분류에서Dev

Why does the SQL connection get blocked randomly?

분류에서Dev

Why values in ArrayList always returns null as first value in Java?

분류에서Dev

Why does UserManager.FindById() return null if I do not relog?

분류에서Dev

Why does NVL2 returns NULL when it's not expected?

분류에서Dev

Order by null first and then in descending order

분류에서Dev

SQL ORDER BY "IF"

분류에서Dev

SQL Select statement - multiple tables allow null values

분류에서Dev

Inserting Multiple values into a single null columns using sql

분류에서Dev

Return null rows for non-matched values in IN clause in SQL

분류에서Dev

Why does this code return undefined 4 times instead of the array values?

분류에서Dev

React Native - Why does displaying components through iteration give the id prop as the last iteration number to all of the components?

분류에서Dev

The SQL Column name or number of supplied values does not match table definition

분류에서Dev

Make Spring Boot JSON enum deserialization strict, so it does not silently convert invalid values into null

분류에서Dev

How to grep recursively in order of last modified time?

Related 관련 기사

  1. 1

    Why does the order of alternatives matter in regex?

  2. 2

    Why does the order of applying advice matter?

  3. 3

    why does the order of variable declaring matter?

  4. 4

    How does work SQL with order by 0?

  5. 5

    Why does SSIS lookup return null value?

  6. 6

    Why does this MySQL stored function return null?

  7. 7

    Replacing null values in dynamic pivot sql query

  8. 8

    Not able to allow null values in my sql script

  9. 9

    Why does `iperf` report huge values for bandwidth?

  10. 10

    Does Play provide any guarantees for queryString values order?

  11. 11

    Why does the malloc order matter when a struct is sufficiently sized?

  12. 12

    Why MySQL does not return rows in same order as appeared in IN clause?

  13. 13

    Why does Order matter in Kwarg parameters in MagicMock asserts?

  14. 14

    Does the order of JOIN vs WHERE in SQL affect performance?

  15. 15

    Why does my linq to sql query fail?

  16. 16

    Why does the SQL connection get blocked randomly?

  17. 17

    Why values in ArrayList always returns null as first value in Java?

  18. 18

    Why does UserManager.FindById() return null if I do not relog?

  19. 19

    Why does NVL2 returns NULL when it's not expected?

  20. 20

    Order by null first and then in descending order

  21. 21

    SQL ORDER BY "IF"

  22. 22

    SQL Select statement - multiple tables allow null values

  23. 23

    Inserting Multiple values into a single null columns using sql

  24. 24

    Return null rows for non-matched values in IN clause in SQL

  25. 25

    Why does this code return undefined 4 times instead of the array values?

  26. 26

    React Native - Why does displaying components through iteration give the id prop as the last iteration number to all of the components?

  27. 27

    The SQL Column name or number of supplied values does not match table definition

  28. 28

    Make Spring Boot JSON enum deserialization strict, so it does not silently convert invalid values into null

  29. 29

    How to grep recursively in order of last modified time?

뜨겁다태그

보관