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>
精彩评论