I am trying to find out the maximum value for an integer (signed or unsigned) from a MySQL database. Is there a way to pull back this information from the database itself?
Are there any built-in constants or functions I can use (either standard SQL or MySQL specific).
At http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html it lists the values – but is there a way for the database to tell me.
The following gives me the MAX_BIGINT – what I’d like is the MAX_INT.
SELECT CAST( 99999999999999999999999 AS SIGNED ) as max_int;
# max_int | 9223372036854775807
In Mysql there is a cheap trick to do this:
the tilde is the bitwise negation. The resulting value is a bigint. See: http://dev.mysql.com/doc/refman/5.1/en/bit-functions.html#operator_bitwise-invert
For the other integer flavours, you can use the right bitshift operator
>>like so: