How to get min/max of two integers in Postgres/SQL?

Posted by HRJ on Stack Overflow See other posts from Stack Overflow or by HRJ
Published on 2010-05-29T19:38:21Z Indexed on 2010/05/29 19:42 UTC
Read the original article Hit count: 238

Filed under:

How do I find the maximum (or minimum) of two integers in Postgres/SQL? One of the integers is not a column value.

I will give an example scenario:

I would like to subtract an integer from a column (in all rows), but the result should not be less than zero. So, to begin with, I have:

UPDATE my_table
SET my_column = my_column - 10;

But this can make some of the values negative. What I would like (in pseudo code) is:

UPDATE my_table
SET my_column = MAXIMUM(my_column - 10, 0);

© Stack Overflow or respective owner

Related posts about postgresql