开发者

Treat Null as Max

开发者 https://www.devze.com 2023-01-10 07:08 出处:网络
So if I had a table in my database wit开发者_Python百科h the values 1 2 3 4 NULL And I executed the query

So if I had a table in my database wit开发者_Python百科h the values

1
2
3
4
NULL

And I executed the query

SELECT MAX(col1) FROM <table>

I'd get 4. Is there any way to change this so Null would be treated as the maximum as oppose to the minimum?

Thanks!


SELECT MAX(ISNULL(col1, 2147483647)) FROM <table> 

[2147483647 = 2^31 - 1]


Just as a variant, you can do this on Oracle.

SELECT *
  FROM ( SELECT col1 
           FROM <table>
          ORDER BY col1 DESC NULLS FIRST
       )
 WHERE rownum = 1

(OP hasn't specified any particular flavour of database)


      SELECT MAX(ISNULL(col1, YouBiggestNumber)) FROM <table>
0

精彩评论

暂无评论...
验证码 换一张
取 消