0

Anybody knows how to convert a big int to int in a MySQL stored procedure.

  1. Solution in this posthttp://stackoverflow.com/questions/11595217/convert-bigint-unsigned-to-int is not working as I tried:

set @x=BigToInt(2147483649); ERROR 1264 (22003): Out of range value for column

  1. I tried this and it doesn't work either:

    DELIMITER // DROP FUNCTION IF EXISTS BigToInt;

    CREATE FUNCTION BigToInt (n BIGINT) RETURNS INTEGER begin DECLARE result INTEGER default 0; set result = CAST(n as SIGNED); return result; end// DELIMITER ;

it gives me same error as #1.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Ping Gong
  • 11
  • 1
  • 2
  • 1
    What part of "Out of range value for column" is a problem here? `INT` can *only* hold values between -2147483648 and 2147483647. 2147483649 is **too big**. If you need numbers that big you need a `BIGINT`, there is no way around it. – tadman Mar 02 '17 at 04:35
  • Unless, *maybe*, `INT UNSIGNED` is suitable here. – eggyal Mar 02 '17 at 04:41

1 Answers1

0

Other option:

DELIMITER //
CREATE FUNCTION BigToInt (n BIGINT) RETURNS VARCHAR(50) 
begin 
DECLARE result VARCHAR(50) default 0; 
set result = CAST(n as signed);
return result;
end
// DELIMITER ;

This will accept a long int but limit to 19 value only.

Integer has a maximum value

Community
  • 1
  • 1
Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30