I made this simple function, and the result returns 1 rather than 0.5
What did I do wrong?
DELIMITER //
DROP FUNCTION IF EXISTS test_decimal //
CREATE FUNCTION test_decimal(input DECIMAL)
RETURNS DECIMAL
BEGIN
SET @_credit = 0.5;
RETURN input * @_credit;
END //
DELIMITER ;
SELECT test_decimal(1);
Because you didn’t specify the
precisionandscalethat’s why it’s rounding the value. The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point. By default, the value of the precision is10and the value of scale is0. So,RETURNS DECIMALis the same asRETURNS DECIMAL(10,0). If the scale is 0, DECIMAL values contain no decimal point or fractional part. Try specifying in to your function.DECIMAL, NUMERIC
SQLFiddle Sample