Trying to create a function in MySQL 5.7 that will return the total sales for a year, formatted with a $, and two decimal places given the year as an integer input.
Table columns InvoiceID, deptid, salsamt, salesdate
DELIMITER $$
CREATE FUNCTION `tot_sale` (Year INT)
RETURNS VARCHAR2
BEGIN
Select concat('$', format(salesamt), 2)
from sales
where YEAR(salesdate) = Year;
RETURN 1;
END$$
DELIMITER ;
2 needs to be the second argument to FORMAT(), not an argument to CONCAT().
If a function uses SELECT, it has to store the result in a variable. Only procedures are allowed to return a result set directly.
You need to use SUM(salesamt) to get the total sales for the year.
DELIMITER $$
CREATE FUNCTION `tot_sale` (Year INT)
RETURNS VARCHAR(255)
BEGIN
DECLARE total VARCHAR(255);
Select concat('$', format(SUM(salesamt), 2))
INTO total
from sales
where YEAR(salesdate) = Year;
RETURN #total;
END$$
DELIMITER ;
Related
In addition to 'price', i would like to declare and return a second column named 'tax', but i don't know how to go about it in the right way.
Here's an example of what i'm trying to achieve,
DELIMITER $$
CREATE FUNCTION calcProfit(cost FLOAT, price FLOAT) RETURNS DECIMAL(9,2)
BEGIN
DECLARE profit DECIMAL(9,2);
DECLARE tax DECIMAL(9,2);
SET profit = price-cost;
SET tax = cost*1.2;
RETURN profit,tax;
END$$
DELIMITER ;
Stored routines are of two kinds, 'procedures' and 'functions'; Functions are used to return a value that you can use in an expression for example:
SELECT sayHello('Frank'); SELECT countName('Frank');
A store procedure is more appropriate for the task. But you probably want to add more complex statements inside this stored routine ..
Anyway this will return a row with profit and tax.
DELIMITER //
CREATE PROCEDURE calcProfit(IN cost FLOAT, price FLOAT)
BEGIN
DECLARE profit DECIMAL(9,2);
DECLARE tax DECIMAL(9,2);
SET profit = price - cost;
SET tax = cost * 1.2;
SELECT profit, tax;
END
//
DELIMITER ;
CALL calcProfit(1,2);
My question is about a function returning only one value but I still get this error, so What I'm supposed to get is average day between the order date and the shipped date, the query is doing that and returning me only one value which is the average. If I use just the SELECT statement outside the of the function I get one column/row answer of 8.4920.
How can I fix that please. Thanks.
DELIMITER //
DROP FUNCTION IF EXISTS OrderFulfilmel//
CREATE FUNCTION OrderFulfilmel(average int) RETURNS DOUBLE Deterministic
BEGIN
SELECT AVG(DATEDIFF(ShippedDate, OrderDate)) AS averageDay
FROM Orders;
END//
DELIMITER ;
You can try below
CREATE FUNCTION OrderFulfilmel(average int) RETURNS DOUBLE Deterministic
BEGIN
DECLARE var_name DECIMAL(10,2);
SET var_name = 0;
SELECT AVG(DATEDIFF(ShippedDate, OrderDate)) INTO var_name
FROM Orders;
RETURN var_name;
END
I don't understand why your function would have an argument. So, I'm thinking:
DELIMITER //
DROP FUNCTION IF EXISTS OrderFulfilmel//
CREATE FUNCTION OrderFulfilmel ()
RETURNS DOUBLE Deterministic
BEGIN
DECLARE #diff DOUBLE;
SELECT #diff := AVG(DATEDIFF(ShippedDate, OrderDate)) AS averageDay
FROM Orders;
RETURN #diff;
END//
DELIMITER ;
DELIMITER $$
CREATE FUNCTION current_age (birthdate date)
RETURNS date
BEGIN
DECLARE age_return date;
DECLARE rightnow date;
SET rightnow = date(now());
SET age_return = timestampdiff(year, rightnow, date(birthdate));
RETURN age_return;
END $$
DELIMITER ;
select current_age(date('2017-06-30'));
I am trying to create a function which years the difference in years. I cant get it to work and I dont understand why, because the following works fine outside the function.
SET #birthdate = '2001-01-01';
SET #rightnow = date(now());
SELECT timestampdiff(year, #rightnow, date(#birthdate))
If we want to return a number of years, then the return type would be a numeric type like INT or DECIMAL. We wouldn't return a DATE datatype.
DELIMITER $$
CREATE FUNCTION current_age(birthdate DATE)
RETURNS INT
BEGIN
RETURN TIMESTAMPDIFF(YEAR,birthdate,DATE(NOW()));
END$$
DELIMITER ;
Directions
Write a proceedure called GetAllCustomerLevelsthat returns the customerNumber, customerName, and cardtype for all customers.
Hint: Use CASE in your select statement. See: Case in Select Statement at stackoverflow or MySQL CASE operator from w3resource. This could also be done using a nested IF. See: Inline IF and CASE statements in MySQL from timmurphy.
Below is an example proceedure that returns the customerNumber and customerName for all customers:
DROP PROCEDURE IF EXISTS GetAllCustomerLevels;
DELIMITER $$
CREATE PROCEDURE GetAllCustomerLevels()
BEGIN
select customerNumber, customerName
from customers;
END$$
DELIMITER ;
Save the sql that created the GetAllCustomerLevels procedure as GetAllCustomerLevels.sql
One of my Attempts
DROP PROCEDURE IF EXISTS GetAllCustomerLevels2;
DELIMITER $$
CREATE PROCEDURE GetAllCustomerLevels2()
BEGIN
DECLARE creditlimit double;
call GetCustomerLevel(#customerNumber,#cardType);
select customerNumber, customerName, #cardType
from customers
end;
SET #cardType =
(CASE
WHEN (creditlimit > 50000) THEN 'PLATINUM'
WHEN (creditlimit <= 50000 AND creditlimit >= 10000) THEN 'GOLD'
WHEN (creditlimit < 10000) THEN 'SILVER'
ELSE 'No card'
END
);
END$$
DELIMITER ;
My result returns customerName and customerNumber but #cardType stays empty.
In addition to 'price', i would like to declare and return a second column named 'tax', but i don't know how to go about it in the right way.
Here's an example of what i'm trying to achieve,
DELIMITER $$
CREATE FUNCTION calcProfit(cost FLOAT, price FLOAT) RETURNS DECIMAL(9,2)
BEGIN
DECLARE profit DECIMAL(9,2);
DECLARE tax DECIMAL(9,2);
SET profit = price-cost;
SET tax = cost*1.2;
RETURN profit,tax;
END$$
DELIMITER ;
Stored routines are of two kinds, 'procedures' and 'functions'; Functions are used to return a value that you can use in an expression for example:
SELECT sayHello('Frank'); SELECT countName('Frank');
A store procedure is more appropriate for the task. But you probably want to add more complex statements inside this stored routine ..
Anyway this will return a row with profit and tax.
DELIMITER //
CREATE PROCEDURE calcProfit(IN cost FLOAT, price FLOAT)
BEGIN
DECLARE profit DECIMAL(9,2);
DECLARE tax DECIMAL(9,2);
SET profit = price - cost;
SET tax = cost * 1.2;
SELECT profit, tax;
END
//
DELIMITER ;
CALL calcProfit(1,2);