
Complete Guide to MySQL Functions: MySQL is not just a database for storing and retrieving data—it also provides powerful built-in functions that allow you to manipulate and format data efficiently. Functions in MySQL return a result after performing specific operations, and they can be categorized into different types based on their functionality.
This article covers string functions, numeric functions, stored functions, and user-defined functions in MySQL with practical examples.
Table of Contents
- What Are MySQL Functions?
- Why Use MySQL Functions?
- Types of MySQL Functions
- String Functions
- Numeric Functions
- Stored Functions
- User-Defined Functions
- MySQL String Functions with Examples
- MySQL Numeric Functions with Examples
- MySQL Stored Functions with Examples
- MySQL User-Defined Functions Overview
- Summary
1. What Are MySQL Functions?
Complete Guide to MySQL Functions: A function in MySQL is a predefined or user-defined operation that performs a calculation and returns a value. Some functions require input parameters, while others do not.
For example, if you want to change the default YYYY-MM-DD date format to DD-MM-YYYY, you can use the built-in DATE_FORMAT function.
sql
SELECT DATE_FORMAT(‘2024-03-21’, ‘%d-%m-%Y’);
2. Why Use MySQL Functions?
- Data Formatting: Functions help in modifying the data format directly within SQL queries.
- Performance Improvement: Using MySQL functions reduces the need for additional programming logic in application code.
- Consistency: Ensures uniform data processing across different applications accessing the same database.
- Reduced Network Load: Instead of sending raw data to the application for processing, MySQL functions return pre-processed data, reducing network traffic.
3. Types of MySQL Functions
A) String Functions
Complete Guide to MySQL Functions: String functions manipulate text-based data in MySQL.
Example: Convert movie titles to uppercase using UCASE():
sql
SELECT movie_id, title, UCASE(title) AS upper_case_title FROM movies;
movie_id | title | upper_case_title |
4 | Code Name Black | CODE NAME BLACK |
5 | Daddy’s Little Girls | DADDY’S LITTLE GIRLS |
7 | Davinci Code | DAVINCI CODE |
Other common string functions:
- LCASE() – Converts text to lowercase.
- CONCAT(str1, str2, …) – Combines multiple strings.
- LENGTH() – Returns the length of a string.
B) Numeric Functions
Complete Guide to MySQL Functions: Numeric functions perform arithmetic operations in MySQL.
Arithmetic Operators in MySQL
Operator | Description | Example | Output |
DIV | Integer division | SELECT 23 DIV 6; | 3 |
/ | Division | SELECT 23 / 6; | 3.8333 |
– | Subtraction | SELECT 23 – 6; | 17 |
+ | Addition | SELECT 23 + 6; | 29 |
* | Multiplication | SELECT 23 * 6; | 138 |
% or MOD | Modulus | SELECT 23 % 6; | 5 |
Common Numeric Functions
FLOOR(value) – Rounds a number down to the nearest integer.
sql
SELECT FLOOR(23 / 6) AS floor_result;
- Output: 3
ROUND(value) – Rounds a number to the nearest whole number.
sql
SELECT ROUND(23 / 6) AS round_result;
- Output: 4
RAND() – Generates a random number.
sql
SELECT RAND() AS random_result;
- Output: 0.84723 (varies every time)
C) Stored Functions
Complete Guide to MySQL Functions: Stored functions in MySQL are user-defined functions created inside the database server and can be reused in SQL queries.
Syntax for Creating a Stored Function
sql
CREATE FUNCTION function_name(parameter1 TYPE)
RETURNS return_type
DETERMINISTIC
BEGIN
— Function logic here
RETURN value;
END;
Example: Checking Overdue Movie Returns
Complete Guide to MySQL Functions: The following function checks whether a rented movie is past its return date:
sql
DELIMITER |
CREATE FUNCTION is_movie_overdue(return_date DATE)
RETURNS VARCHAR(3)
DETERMINISTIC
BEGIN
DECLARE result VARCHAR(3);
IF CURDATE() > return_date THEN
SET result = ‘Yes’;
ELSE
SET result = ‘No’;
END IF;
RETURN result;
END |
DELIMITER ;
Using the Function in a Query
sql
SELECT movie_id, return_date, CURDATE(), is_movie_overdue(return_date) FROM movierentals;
movie_id | return_date | CURDATE() | is_movie_overdue |
2 | 2024-02-15 | 2024-03-21 | Yes |
3 | 2024-04-10 | 2024-03-21 | No |
D) User-Defined Functions (UDFs)
Complete Guide to MySQL Functions: Unlike stored functions, user-defined functions (UDFs) are created outside MySQL using languages like C or C++ and then integrated into the database.
UDFs provide additional functionality not available in built-in MySQL functions. They must be compiled and installed in MySQL before use.
8. Summary
- Complete Guide to MySQL Functions: MySQL Functions enhance data manipulation capabilities within SQL queries.
- Built-in functions include string functions (e.g., UCASE()), numeric functions (e.g., ROUND()), and date functions (e.g., DATE_FORMAT()).
- Stored functions allow users to define reusable logic inside MySQL.
- User-defined functions (UDFs) extend MySQL functionality using external programming languages.
Using MySQL functions efficiently can improve database performance, reduce application processing load, and maintain data consistency across multiple systems.
For more advanced MySQL tutorials, keep exploring!
Title: MySQL Functions Explained: String, Numeric, User-Defined & Stored Functions