Q:

Is there a 'greatest' function in db2?

I found in MYSQL and apparently other database engines that there is a "greatest" function that can be used like: greatest(1, 2, 3, 4), and it would return 4.  I need this, but I am using IBM's DB2.  Does anybody know of such an equivalent function, even if it only accepts 2 parameters?
I found somewhere that MAX should do it, but it doesn't work... it only works on selecting the MAX of a column.
If there is no such function, does anybody have an idea what a stored procedure to do this might look like?  (I have no stored procedure experience, so I have no clue what DB2 would be capable of).

A:

Why does MAX not work for you?
select max(1,2,8,3,1,7) from sysibm.sysdummy1
gives me
    1
    ---------------
                 8

      1 record(s) selected.

A:

As Dave points out, MAX should work as it's overloaded as both a scalar and a column function (the scalar takes 2 or more arguments). This is the case in DB2 for LUW, DB2 for z/OS and DB2 for i5/OS. What exact version and platform of DB2 are you using, and what is the exact statement you are using? One of the requirements of the scalar version of MAX is that all the arguments are "compatible" - I suspect there may be a subtle type difference in one or more of the arguments you're passing to the function.

