Simple usage examples of MySQL custom functions

Simple usage examples of MySQL custom functions

This article uses examples to illustrate the usage of MySQL custom functions. Share with you for your reference, the details are as follows:

Let's start with a simple one. Create a function to convert a datetime in the format of '2009-06-23 00:00:00' to a datetime in the format of '2009 June 23, 00:00':

DELIMITER $$
DROP FUNCTION IF EXISTS `sp_test`.`getdate`$$
CREATE FUNCTION `sp_test`.`getdate`(gdate datetime) RETURNS varchar(255)
BEGIN
DECLARE x VARCHAR(255) DEFAULT '';
SET x = date_format(gdate,'%Y year %m month %d day %h hour %i minute %s second');
RETURN x;
END $$
DELIMITER ;

Analysis:

The first sentence defines an end identifier. Because MySQL uses a semicolon as the end identifier of SQL statements by default, and a semicolon is used inside the function body, it will conflict with the default SQL end identifier. Therefore, you need to define another symbol as the SQL end identifier first.

The second sentence is if the function already exists, delete it, sp_test is the name of the database, the function is associated with the database, getdate is the name of the function;

The third sentence creates a function. The parentheses contain the name and type of the parameter. RETURNS defines the type of the function's return value.

The function body must be placed between BEGIN END;

DECLARE is used to define variables in the function body. Here, a variable x is defined, which is empty by default. Then SET is used to assign a value to the variable x.

RETURN is the return value. Here, the variable x is returned. The type of x must be consistent with the return type defined in the third sentence.

Call:

SELECT getdate('2009-06-23 00:00:00');

Returns '2009-06-23 00:00:00'

Branch structure

DELIMITER $$
DROP FUNCTION IF EXISTS `sp_test`.`cutString` $$
CREATE FUNCTION `sp_test`.`cutString`(s VARCHAR(255),n INT) RETURNS varchar(255)
BEGIN
IF(ISNULL(s)) THEN RETURN '';
ELSEIF CHAR_LENGTH(s)<n THEN RETURN s;
ELSEIF CHAR_LENGTH(S)=n THEN RETURN 'equal';
ELSE RETURN CONCAT(LEFT(s,n),'...');
END IF;
END $$
DELIMITER ;

Analysis:

This is a function for extracting a string. It has two parameters, one is a string s and the other is a number n. It keeps the first n bits of string s. If the number of bits in the string is less than n, it returns string s. If it is greater than n, it returns the first n bits followed by...

Call:

SELECT cutString('abcdefghijklmnopqrstuvwxyz',5);

Returns 'abced...'

Loop Structure

DELIMITER $$
DROP FUNCTION IF EXISTS `sp_test`.`morestar`$$
CREATE FUNCTION `sp_test`.`morestar`(n INT) RETURNS text
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE s TEXT DEFAULT '';
myloop:LOOP
SET i=i+1;
SET s = CONCAT(s,'*');
IF i > n THEN LEAVE myloop;
END IF;
END LOOP myloop;
RETURN s;
END $$
DELIMITER ;

Analysis: Generate n *.

Call:

SELECT morestar(5);

return '*****'

Readers who are interested in more MySQL-related content can check out the following topics: "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • MySQL custom function CREATE FUNCTION example
  • In-depth explanation of creating custom functions and stored procedures in MySQL
  • MySQL Chinese character conversion pinyin custom function and usage examples (first letter of the first word)
  • A brief discussion on mysql custom functions
  • How to use custom functions to extract numbers from strings in MySQL
  • Problems with creating custom functions in mysql
  • Detailed explanation of the definition and usage of MySQL stored functions (custom functions)
  • MYSQL custom function to determine whether it is a positive integer example code
  • Detailed explanation of MySQL custom functions and stored procedures
  • MySQL uses custom functions to recursively query parent ID or child ID
  • Analysis of the principle and usage of MySQL custom functions

<<:  Diagram of the Hyperledger Fabric 1.4 environment construction process under Windows 10

>>:  Detailed explanation of Strict mode in JavaScript

Recommend

Example of implementing TikTok text shaking effect with CSS

In daily development, front-end students often ar...

Graphical introduction to the difference between := and = in MySQL

The difference between := and = = Only when setti...

Detailed explanation of explain type in MySQL

Introduction: In many cases, many people think th...

Vue Virtual DOM Quick Start

Table of contents Virtual DOM What is virtual dom...

CSS flex several multi-column layout

Basic three-column layout .container{ display: fl...

Teach you how to use webpack to package and compile TypeScript code

TypeScript Bundling webpack integration Usually, ...

Linux beginners in virtual machines configure IP and restart the network

For those who are new to virtual machines or have...

Linux kernel device driver advanced character device driver notes

/****************** * Advanced character device d...

Detailed explanation of how to create an updateable view in MySQL

This article uses an example to describe how to c...

How to deploy SpringBoot project using Dockerfile

1. Create a SpringBooot project and package it in...

How to configure Nginx domain name rewriting and wildcard domain name resolution

This article introduces how to configure Nginx to...

How to retrieve password for mysql 8.0.22 on Mac

Mac latest version of MySQL 8.0.22 password recov...