This is my query that basically takes two numbers that adds them and multiplies the sum by 10
DELIMITER $$
CREATE FUNCTION tot(a int(4),b INT(4)) RETURNS INT(4)
BEGIN
RETURN ROUND((a+b)*10/9);
END $$
DELIMITER ;
everything is working fine , but I was wondering if there was a way I could add an IF ELSE
that checks if any of of the values entered is null and if so the null value is assigned a value of zero
I've tried this but I'm getting an error
DELIMITER $$
CREATE FUNCTION tot(a int(4),b INT(4)) RETURNS INT(4)
BEGIN
IF (a = "") then
a=0;
ELSE IF (b = "")
b=0;
ELSE
END IF;
RETURN ROUND((a+b)*10/9);
END $$
DELIMITER ;
I was wondering if there was a way I could add an IF ELSE that checks if any of of the values entered is null and if so the null value is assigned a value of zero
You can just use coalesce() to assign a default to null parameters:
DELIMITER $$
CREATE FUNCTION tot(a int(4),b INT(4)) RETURNS INT(4)
BEGIN
RETURN ROUND((COALESCE(a, 0) + COALESCE(b, 0)) * 10 / 9);
END $$
DELIMITER ;
Related
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
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.
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
Define procedure:
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
proc_label:BEGIN
IF tablename IS NULL THEN
SELECT 'Null detect';
LEAVE proc_label;
END IF;
SELECT 'after';
END;
$$
DELIMITER ;
Call Procedure:
CALL SP_Reporting();
Error :
ERROR 1318 (42000): Incorrect number of arguments for PROCEDURE
cds.SP_Reporting ; expected 1, got 0
How pass var by default like SP_Reporting(IN tablename = 'default value' VARCHAR(20))
When you are making your stored procedure, you can assign a value to your input, so there is no need to pass parameters while you are calling the proc.
We usually assign NULL and for making parameters optional, we use this method.
tablename VARCHAR(20) = NULL
Your complete script:
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20) = NULL)
proc_label:BEGIN
IF tablename IS NULL THEN
SELECT 'Null detect';
LEAVE proc_label;
END IF;
SELECT 'after';
END;
$$
DELIMITER ;
EDIT
MySQL is not accepting optional parameters. So one way is to pass NULL value in your stored procedure and check it with IF statement inside your proc.
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
proc_label:BEGIN
IF tablename IS NULL THEN
-- Do something;
ELSE
-- Do something else;
END IF;
END;
$$
DELIMITER ;
You have to pass the table name in the procedure call statement like:
CALL SP_Reporting(table_name);
you can't pass default in call statement.
You can assign default value before calling the procedure.
or use OUT instead of IN as a parameter.
you miss the parameter :tablename
you should like this :
call SP_Reporting('any varchar')
or
call SP_Reporting(null)
I have procedure in MySQL called 'luhn_'.
I would like to use it like function in my other procedure - in IF condition. So it should return me some output parameter (maybe true / false?)
What is the easiest way to implement something like that?
DELIMITER $$
DROP procedure if exists `luhn_`$$
CREATE procedure `luhn_` (IN input_string VARCHAR(256), IN input_lenght INTEGER)
BEGIN
SET #luhn_string = LEFT(TRIM(input_string), input_lenght);
IF(LENGTH(#luhn_string)=input_lenght)
THEN SELECT count(*) from courrier_envoye; -- true
ELSE
SELECT * from courrier_envoye; -- false
END IF;
END$$
-- call luhn_('123456789',10);
if condition
...
if(luhn_('123456789',10) = true) THEN ...
ELSE ...
...
Creating a function would do it for you
DELIMITER $$
CREATE FUNCTION `luhn_`(input_string VARCHAR(256), input_lenght INTEGER) RETURNS tinyint(1)
BEGIN
SET #luhn_string = LEFT(TRIM(input_string), input_lenght);
IF(LENGTH(#luhn_string)=input_lenght) THEN
return true;
ELSE
return false;
END IF;
END
Usage in a stored procedure
if(select luhn_(input_string,input_lenght)) then
select 'a';
else
select 'b';
end if;