MySQL | Creating stored function Last Updated : 18 Jan, 2022 Comments Improve Suggest changes Like Article Like Report The CREATE FUNCTION statement is used for creating a stored function and user-defined functions. A stored function is a set of SQL statements that perform some operation and return a single value. Just like Mysql in-built function, it can be called from within a Mysql statement. By default, the stored function is associated with the default database. The CREATE FUNCTION statement require CREATE ROUTINE database privilege. Syntax: The syntax for CREATE FUNCTION statement in Mysql is: CREATE FUNCTION function_name(func_parameter1, func_parameter2, ..) RETURN datatype [characteristics] func_body Parameters used: function_name: It is the name by which stored function is called. The name should not be same as native(built_in) function. In order to associate routine explicitly with a specific database function name should be given as database_name.func_name.func_parameter: It is the argument whose value is used by the function inside its body. You can't specify to these parameters IN, OUT, INOUT. The parameter declaration inside parenthesis is provided as func_parameter type. Here, type represents a valid Mysql datatype.datatype: It is datatype of value returned by function.characteristics: The CREATE FUNCTION statement is accepted only if at least one of the characteristics { DETERMINISTIC, NO SQL, or READS SQL DATA } is specified in its declaration. func_body is the set of Mysql statements that perform operation. It's structure is as follows: BEGIN Mysql Statements RETURN expression; END The function body must contain one RETURN statement. Example: Consider following Employee Table- emp_idfnamelnamestart_date1MichaelSmith2001-06-222SusanBarker2002-09-123RobertTvler2000-02-094SusanHawthorne2002-04-24 We have to find the number of years the employee has been in the company- DELIMITER // CREATE FUNCTION no_of_years(date1 date) RETURNS int DETERMINISTIC BEGIN DECLARE date2 DATE; Select current_date()into date2; RETURN year(date2)-year(date1); END // DELIMITER ; Calling of above function: Select emp_id, fname, lname, no_of_years(start_date) as 'years' from employee; Output: emp_idfnamelnameyears1MichaelSmith182SusanBarker173RobertTvler194SusanHawthorne17 Comment More infoAdvertise with us Next Article MySQL | Creating stored function T Tanvi_Garg Follow Improve Article Tags : DBMS SQL mysql Similar Reads CEILING() Function in MySQL CEILING() function :This function in MySQL is used to return the smallest integer value that is greater than or equal to a specified number. For example, if the specified number is 4.6, this function will return the integer value of 5 that is greater than 4.6 or if a specified number is 5, this func 2 min read MySQL | PASSWORD Function The MySQL PASSWORD function is used for the generation of a hashed password using a plain-text password string It uses hashing techniques to generate the hashed password. This function is carried out by the authentication system. MySQL server uses the PASSWORD function to encrypt MySQL passwords for 1 min read CURTIME() function in MySQL CURTIME() function in MySQL is used to check the current time. It returns the current time as a value in âhh:mm:ssâ or 'hhmmss' format, depending on whether the function is used in a string or numeric context. Syntax : CURTIME(fsp) Parameters : This method accepts only one parameter. fsp - It specif 2 min read MySQL CASE() Function MySQL CASE function is a conditional statement that returns a value when the first condition is met. Once a condition is met, the CASE function does not check for other conditions. If no condition is met it returns the output in ELSE part. CASE Function in MySQLThe CASE Function in MySQL allows usin 4 min read MySQL | CONVERT( ) Function The MySQL CONVERT() function is used for converting a value from one datatype to a different datatype. The MySQL CONVERT() function is also used for converting a value from one character set to another character set. It accepts two parameters which are the input value and the type to be converted in 2 min read CURRENT_TIMESTAMP() function in MySQL CURRENT_TIMESTAMP() function in MySQL is used to check the current date and time value. In MySQL function, CURRENT_TIMESTAMP is needed when you need to keep a record of the exact time of delivery which is helpful in improving the services and functionalities of any organization. The format of this f 2 min read ELT() Function in MySQL In this article, we are going to cover ELT function with examples. In ELT function, number field will state that how many strings will be there. ELT function in MySQL is used to returns the string which is at index number specified in the argument list. In this function there is number field and str 1 min read SECOND() Function in MySQL SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59. In this fu 2 min read MySQL | BINARY Function The MySQL BINARY function is used for converting a value to a binary string. The BINARY function can also be implemented using CAST function as CAST(value AS BINARY). The BINARY function accepts one parameter which is the value to be converted and returns a binary string. Syntax: BINARY value Parame 1 min read COT() Function in MySQL COT() function : This function in MySQL is used to return the cotangent of a specified number. If the specified number is 0, an error or NULL will be returned. In a right triangle, the cotangent of an angle is the length of it's adjacent side divided by the length of the opposite side. Similarly, th 1 min read Like