MySQL function giving same result - mysql

I have created the following function in MySQL and it always give me the result as A whatever the integer number I give as parameter value.
DELIMITER $$
DROP FUNCTION IF EXISTS ISICN4A;
CREATE FUNCTION cdn(coden INT)
RETURNS CHAR
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE coden INT UNSIGNED DEFAULT 0;
DECLARE ret CHAR;
SET ret=CASE WHEN coden<=50 then 'A'
when coden BETWEEN 51 AND 100 then 'B'
when coden BETWEEN 101 AND 350 then 'C'
when coden BETWEEN 351 AND 355 then 'D'
when coden>355 then 'E'
ELSE NULL
END;
RETURN ret;
END;$$
DELIMITER $$
Appreciate your kind review and suggestion to modify it.

Your function accepts a parameter coden INT, but in function body you declare a variable with the same name as your parameter.
Try to remove DECLARE coden INT UNSIGNED DEFAULT 0; from your function body
so that your function will look like:
DELIMITER $$
DROP FUNCTION IF EXISTS ISICN4A;
CREATE FUNCTION cdn(coden INT) RETURNS CHAR
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE ret CHAR;
SET ret=CASE WHEN coden<=50 THEN 'A'
WHEN coden BETWEEN 51 AND 100 THEN 'B'
WHEN coden BETWEEN 101 AND 350 THEN 'C'
WHEN coden BETWEEN 351 AND 355 THEN 'D'
WHEN coden>355 THEN 'E'
ELSE NULL
END;
RETURN ret;
END;$$

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

Mysql bigint to int

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

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: use "IF NOT IN" in procedure

I'm trying to check class using MySQL procedure, but the following procedure always returns 0:
DELIMITER //
CREATE PROCEDURE `validate_class`(IN `class` INT)
BEGIN
if(class NOT IN ('A','B','E') ) then
select 1;
else
select 0;
end if;
END //
DELIMITER ;
call test:
call validate_class('G'); //return 0
call validate_class('A'); //return 0
It should return 1 when class isn't (A and B and E), any help?
You have implicit conversions CHAR -> INT -> CHAR.
Change parameter datatype:
CREATE PROCEDURE `validate_class`(IN `class` CHAR(1))
BEGIN
if(class NOT IN ('A','B','E') ) then
select 1;
else
select 0;
end if;
END
SqlFiddleDemo

No return found in function

I have browsed around and other solutions don't seem to be working for me here. I keep getting a 'No return found for functon' error when trying to create this function in MySQL. Any idea why?
CREATE FUNCTION `mydb`.`myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
SELECT SUM(Transaction.Bought) INTO #Qty FROM Transaction WHERE Transaction.Name = Name;
RETURN #Qty
Try this
DELIMITER $$
CREATE FUNCTION `myfunction`(`Name` VARCHAR(20) CHARSET utf8) RETURNS INT NOT DETERMINISTIC
READS SQL DATA
MAIN: BEGIN
DECLARE returnVal int;
SELECT SUM(`Transaction`.Bought) INTO returnVal FROM `Transaction` WHERE `Transaction`.Name = Name;
RETURN returnVal;
END MAIN;$$
DELIMITER ;
Alternatively, try this,
DELIMITER $$
CREATE FUNCTION `myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
BEGIN
DECLARE returnVal int;
SELECT SUM(`Transaction`.Bought) INTO returnVal
FROM `Transaction`
WHERE `Transaction`.Name = Name;
RETURN returnVal;
END$$
DELIMITER ;
delimiter //
create function myfun1234 (i int,j int)
returns int
begin
declare x int ;
declare y int ;
declare z int ;
set x=10;
set y=20;
if (1<=x && j>=y )then
set z=i+j;
end if;
return z;
end; //
-- error1 FUNCTION myfun12 ended without RETURN