How do you return 0 instead of null when running the following command:
SELECT MAX(X) AS MaxX
FROM tbl
WHERE XID = 1
(Assuming there is no row where XID=1)
How do you return 0 instead of null when running the following command:
SELECT MAX(X) AS MaxX
FROM tbl
WHERE XID = 1
(Assuming there is no row where XID=1)
8条答案
按热度按时间4ktjp1zp1#
or:
9avjhtql2#
In SQL 2005 / 2008:
odopli943#
Like this (for MySQL):
For MSSQL replace
IFNULL
withISNULL
or for Oracle useNVL
bf1o4zei4#
You can also use COALESCE ( expression [ ,...n ] ) - returns first non-null like:
mm5n2pyu5#
Oracle would be
gxwragnw6#
For OLEDB you can use this query:
As IFNULL is not working there
cnwbcb6i7#
Depends on what product you're using, but most support something like
or
yqkkidmi8#
For my case using
max()
was creating problem with group by even on outer SELECT statement.So only thing that saved my day was following by avoiding adding other columns in group by clause or using aggregate on other columns. So I wrote on outer SELECT statement as following:
Even not directly related to this I hope it will help other people.