Mysql bigint to int - mysql

Anybody knows how to convert a big int to int in a MySQL stored procedure.
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
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.

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

Related

mysql custom function syntax

hi this is my first post here. A novice web development student.
so this first function checks to see if a string contains 'cabo verde' or 'cambodia' or 'cameroon' if yes then just return the original string. otherwise if the string begins with CA regardless of upper or lower case then it returns the string 'CAN'. I think i am getting some syntax wrong as this is my first time creating custom functions in mysql.
DELIMITER $$
create function cleanstr(
string1 VARCHAR(60)
)
returns VARCHAR(60)
DETERMINISTIC
BEGIN
IF(LOWER(string1) = 'cabo verde' OR LOWER(string1) = 'cambodia' OR LOWER(string1) = 'cameroon')
THEN return string1;
ELSE IF (REGEXP_LIKE(string1,'^ca','i') = 1)
THEN return 'CAN';
END IF;
END; $$
DELIMITER ;
this function takes 3 integer values. if first two are null then it returns both those variables equal to 0. If the 3rd variable is NULL then it throws a 45000 error.
DELIMITER $$
create function ints(
num1 int, num2 int, num3 int
)
returns int
DETERMINISTIC
BEGIN
IF(ISNULL(num1) = 1 AND ISNULL(num2) = 1)
THEN
return num1 = 0 AND num2 = 0;
ELSE IF (ISNULL(num3) = 1)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Third value is NULL';
END IF;
END; $$
DELIMITER ;
I can't figure out what exactly i am missing in these functions. Hope it is something small. Thanks

my function is returning nothing

DELIMITER //
DROP FUNCTION IF EXISTS TotalProductionCost;
CREATE FUNCTION TotalProductionCost(Abuilt int(10),Ucost Decimal(5,2) )
RETURNS Decimal(5,2)
BEGIN
DECLARE TotalCost Decimal(5,2);
SET TotalCost = Abuilt * Ucost;
RETURN TotalCost;
END //
DELIMITER ;
SELECT TotalProductionCost(10,1000) AS TotalCost;
I have created above function when Execute it in mysql workbench it worked fine query executed,but when I called function,it didnt return anything.. query executed suceefully.
The sizing of your decimal is exploding based on your input and output of (5,2).
The following works:
DROP FUNCTION IF EXISTS TotalProductionCost;
DELIMITER $$
CREATE FUNCTION TotalProductionCost(Abuilt int,Ucost DECIMAL(12,2) )
RETURNS Decimal(12,2)
DETERMINISTIC
BEGIN
DECLARE TotalCost Decimal(12,2);
SET TotalCost = Abuilt * Ucost;
RETURN (TotalCost);
END $$
DELIMITER ;
test:
SELECT TotalProductionCost(10,1000.12) AS TotalCost;
So, yes, I confirmed yours choked. And you need to be careful with your sizing for the in and return.

mysql declare variable syntax error

I am trying to migrate a scalar valued function of MS SQL to function in MySQL and have been using the following syntax as Queries to create function named xx4.
DROP FUNCTION IF EXISTS `xx4`;`
CREATE DEFINER = CURRENT_USER FUNCTION `xx4`(code VARCHAR(3))`
RETURNS CHAR`
BEGIN
`DECLARE coden INT UNSIGNED DEFAULT 0= CAST(CODE AS UNSIGNED)`
`DECLARE ret CHAR =CASE`
`when coden<50 then 'A'`
`when coden<100 then 'B'`
`when coden<350 then 'C'`
`when coden<360 then 'D'`
`else null`
`END`
`RETURN ret`
`END;
It gives me error as:
at Declare ret char=Case when coden<50 then 'A' when coden<100 w' at line 4
Can you please tell me where I am wrong? Appreciate your help.
I'm not sure why there are so many backticks, but the following syntax works (mind your semicolons):
DROP FUNCTION IF EXISTS xx4;
DELIMITER $$
CREATE DEFINER = CURRENT_USER FUNCTION xx4(`code` VARCHAR(3))
RETURNS CHAR
BEGIN
DECLARE coden INT UNSIGNED DEFAULT 0 = CAST(`CODE` AS UNSIGNED);
DECLARE ret CHAR;
set ret = CASE
when coden<50 then 'A'
when coden<100 then 'B'
when coden<350 then 'C'
when coden<360 then 'D'
else null
END;
RETURN ret;
END;$$
DELIMITER ;

MySQL syntax (function) is error

CREATE FUNCTION FC_IDKRITERIA()
RETURNS CHAR(3)
AS
BEGIN
DECLARE #MAX INT , #KODEBARU CHAR(3)
SELECT #MAX = MAX (RIGHT(IDKRITERIA,2)) FROM KRITERIA
IF #MAX IS NULL
SET #MAX = 0
SET #KODEBARU = 'K' + RIGHT('0'+CONVERT(VARCHAR(3),#MAX+ 1 ) ,2)
RETURN #KODEBARU
END
Every statement in a procedure must end with ;. To keep this from ending the function definition, use the DELIMITER command to change the command delimiter to something else.
And when doing a variable assignment in a SELECT clause, you have to use :=.
There's no AS at the beginning of a function definition.
You don't declare variables that begin with #.
You need THEN and END IF in an IF statement.
To concatenate strings, use CONCAT(), not +.
You have the arguments to CONVERT() in the wrong order, and VARCHAR(3) is not a valid type argument, it should be CHAR(3).
In a function, you can't use a SELECT statement at the top-level, because that means to return the result set, and functions can only return single values. So you have to assign #MAX from a (SELECT ...) expression.
DELIMITER $$
CREATE FUNCTION FC_IDKRITERIA() RETURNS CHAR(3)
BEGIN
SET #MAX = (SELECT MAX (RIGHT(IDKRITERIA,2)) FROM KRITERIA);
IF #MAX IS NULL
THEN SET #MAX = 0;
END IF;
SET #KODEBARU = CONCAT('K', RIGHT('0'+CONVERT(#MAX+ 1, CHAR(3)) ,2));
RETURN #KODEBARU;
END;
$$
DELIMITER ;

mysql select into returns NULL

Please help me understand,
I need to make stored procedure for creating new user into db.
delimiter //
CREATE PROCEDURE add_user (x VARCHAR(25))
BEGIN
DECLARE x VARCHAR(25) DEFAULT 'mark';
DECLARE newname VARCHAR(25);
DECLARE xid INT;
SELECT x, id INTO newname, xid
FROM users WHERE x = x;
SELECT newname;
END;
delimiter ;
when i call add_user('peter');
it shows me:
newname/null
where do i go wrong ?
CREATE PROCEDURE add_user (x VARCHAR(25))
BEGIN
DECLARE x VARCHAR(25) DEFAULT 'mark';
This actually creates two variables named x. The first is the function parameter, and the second is a local variable which is in scope inside the BEGIN ... END block. According to MySQL's scoping rules, only one of these variables can be visible, which means that the parameter x is hidden and inaccessible inside the BEGIN ... END block.
The solution is simply to delete the line
DECLARE x VARCHAR(25) DEFAULT 'mark';
and, if you need to assign a default value, do it in an IF block:
IF x IS NULL THEN
SET x = 'mark';
END IF;
The complete function definition should be
delimiter //
CREATE PROCEDURE add_user (x VARCHAR(25))
BEGIN
DECLARE newname VARCHAR(25);
DECLARE xid INT;
IF x IS NULL THEN
SET x = 'mark';
END IF;
SELECT x, id INTO newname, xid
FROM users WHERE x = x;
SELECT newname;
END;
delimiter ;