4.2.35. MySQL function

发布时间 :2025-10-25 12:31:20 UTC      

MySQL has many built-in functions, and the descriptions of these functions are listed below.

MySQL string function

Function

Description

Example

ASCII (s)

Returns the ASCII code of the first character of the string s.

Returns the ASCII code of the first letter of the CustomerName field:

SELECT 
ASCII(CustomerName) 
AS   
NumCodeOfFirstChar
FROM Customers;

CHAR_LENGTH (s)

Returns the number of characters of the string s

Returns the number of characters of the string RUNOOB

SELECT CHAR_LENGTH("RUNOOB") AS LengthOfString;

CHARACTER_LENGTH (s)

Returns the number of characters in the string s, equivalent to CHAR_LENGTH (s)

Returns the number of characters of the string RUNOOB

SELECT CHARACTER_LENGTH("RUNOOB") AS LengthOfString;

CONCAT (s _ 1 ~ 2. S _ n)

A number of strings, such as the string S1, and so on, are merged into a single string.

Merge multiple strings

SELECT CONCAT("SQL ", "Runoob ", "Gooogle ", "Facebook") AS ConcatenatedString;

CONCAT_WS (x, s _ 1 ~ s _ 2… s _ n)

Same as CONCAT (S1 and S2…) Function, but each string should be separated by a delimiter x

Merge multiple strings and add delimiters:

SELECT CONCAT_WS("-", "SQL", "Tutorial", "is", "fun!")AS ConcatenatedString;

FIELD (sjournal s 1 ~ 2…)

Returns the first string s in the string list (S1 ~ S2…) Position in

Returns the position of the string c in the list value

SELECT FIELD("c", "a", "b", "c", "d", "e");

FIND_IN_SET (S1 and S2)

Returns the position of the string that matches S1 in the string S2

Returns the position of the string c in the specified string

SELECT FIND_IN_SET("c", "a,b,c,d,e");

FORMAT (XMagne n)

The function can format the number x to n places after the decimal point, and the last digit is rounded.

Format the numeral “#, # #. # #” form:

SELECT FORMAT(250500.5634, 2);     -- 输出 250,500.56

INSERT (s1 ~ (1) ~ (x) ~ () ~ (2))

The string S2 replaces the x position of S1, starting with a string of length len

Replace the six characters starting at the first position of the string with runoob:

SELECT INSERT("google.com", 1, 6, "runoob");  -- 输出:runoob.com

LOCATE (s 1m s)

Get the start position of S1 from the string s

Get the position of b in the string abc:

SELECT LOCATE('st','myteststring');  -- 5

Returns the position of b in the string abc:

SELECT LOCATE('b', 'abc') -- 2

LCASE (s)

Change all letters of the string s to lowercase letters

Convert the string RUNOOB to lowercase:

SELECT LCASE('RUNOOB') -- runoob

LEFT (sforme n)

Returns the first n characters of the string s

Returns the first two characters in the string runoob:

SELECT LEFT('runoob',2) -- ru

LOWER (s)

Change all letters of the string s to lowercase letters

Convert the string RUNOOB to lowercase:

SELECT LOWER('RUNOOB') -- runoob

LPAD (S1 Lenpender S2)

Fill the string S2 at the beginning of string S1 to make the string length reach len

Populate the string xx to the beginning of the abc string:

SELECT LPAD('abc',5,'xx') -- xxabc

LTRIM (s)

Remove the space at the beginning of the string s

Remove the space at the beginning of the string RUNOOB:

SELECT LTRIM("    RUNOOB") AS LeftTrimmedString;-- RUNOOB

MID (spaper n. Len)

Truncate a substring of length len from the n position of the string s, which is the same as SUBSTRING.

Intercept 3 characters from the second position in the string RUNOOB:

SELECT MID("RUNOOB", 2, 3) AS ExtractString; -- UNO

POSITION (S1 IN s)

Get the start position of S1 from the string s

Returns the position of b in the string abc:

SELECT POSITION('b' in 'abc') -- 2

REPEAT (sforme n)

Repeat the string s n times

Repeat the string runoob three times:

SELECT REPEAT('runoob',3) -- runoobrunoobrunoob

REPLACE (SMagazine S1, Persons2)

Replace string S1 in string s with string S2

Replace the character an in the string abc with the character x:

SELECT REPLACE('abc','a','x') --xbc

REVERSE (s)

Reverse the order of the string s

Reverse the order of the string abc:

SELECT REVERSE('abc') -- cba

RIGHT (sforme n)

Returns the last n characters of the string s

Returns the last two characters of the string runoob:

SELECT RIGHT('runoob',2) -- ob

RPAD (S1 Lenpender S2)

Add the string S2 at the end of string S1 to make the length of the string reach len

Populate the string xx to the end of the abc string:

SELECT RPAD('abc',5,'xx') -- abcxx

RTRIM (s)

Remove the space at the end of the string s

Remove the space at the end of the string RUNOOB:

SELECT RTRIM("RUNOOB     ") AS RightTrimmedString;   -- RUNOOB

SPACE (n)

Return n spaces

Return 10 spaces:

SELECT SPACE(10);

STRCMP (S1 and S2)

Compare the strings S1 and S2, return 0 if S1 is equal to S2, return 1 if S1 > S2, and return-1 if S1 < S2

Compare strings:

SELECT STRCMP("runoob", "runoob");  -- 0

SUBSTR (s, start, length)

Truncate a substring of length length from the start position of the string s

Intercept 3 characters from the second position in the string RUNOOB:

SELECT SUBSTR("RUNOOB", 2, 3) AS ExtractString; -- UNO

SUBSTRING (s, start, length)

Truncate a length substring from the start position of the string s, which is equivalent to SUBSTR (s, start, length)

Intercept 3 characters from the second position in the string RUNOOB:

SELECT SUBSTRING("RUNOOB", 2, 3) AS ExtractString; -- UNO

SUBSTRING_INDEX (s, delimiter, number)

Returns the substring after the delimiter delimiter that appears after the number of the string s. If number is positive, returns the string to the left of the number character. If number is negative, returns the string to the right of the first (absolute value of number (from the right).

SELECT SUBSTRING_INDEX('a*b','*',1) -- a
SELECT SUBSTRING_INDEX('a*b','*',-1)    -- b
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('a*b*c*d*e','*',3),'*',-1)    -- c

TRIM (s)

Remove the spaces at the beginning and end of the string s

Remove the leading and trailing spaces from the string RUNOOB:

SELECT TRIM('    RUNOOB    ') AS TrimmedString;

UCASE (s)

Convert a string to uppercase

Convert the string runoob to uppercase:

SELECT UCASE("runoob"); -- RUNOOB

UPPER (s)

Convert a string to uppercase

Convert the string runoob to uppercase:

SELECT UPPER("runoob"); -- RUNOOB

MySQL digital function

Function name

Description

Example

ABS (x)

Returns the absolute value of x

Returns the absolute value of-1:

SELECT ABS(-1) -- 返回1

ACOS (x)

Find the inverse cosine of x (in radians), x is a numerical value

SELECT ACOS(0.25);

ASIN (x)

Find the arc sine (in radians), x is a numeric value

SELECT ASIN(0.25);

ATAN (x)

Find the arc tangent (in radians), x is a numerical value

SELECT ATAN(2.5);

ATAN2 (n, m)

Find the inverse tangent (in radians)

SELECT ATAN2(-0.8, 2);

AVG (expression)

Returns the average of an expression, where expression is a field

Returns the average of the Price fields in the Products table:

SELECT AVG(Price) AS AveragePrice FROM Products;

CEIL (x)

Returns the smallest integer greater than or equal to x

SELECT CEIL(1.5) -- 返回2

CEILING (x)

Returns the smallest integer greater than or equal to x

SELECT CEILING(1.5); -- 返回2

COS (x)

Find the cosine (the parameter is radians)

SELECT COS(2);

COT (x)

Find the cotangent (the parameter is radians)

SELECT COT(6);

COUNT (expression)

Returns the total number of records for the query. The expression parameter is a field or * number.

Returns the total number of records in the products field in the Products table:

DEGREES (x)

Convert radians to angl

SELECT DEGREES(3.1415926535898) -- 180

N DIV m

Divisible, n is the divisor, m is the divisor

Calculate 10 divided by 5:

EXP (x)

Return to the x power of e

Calculate the third power of e:

FLOOR (x)

Returns the largest integer less than or equal to x

An integer less than or equal to 1.5:

GREATEST (expr1, expr2, expr3,…)

Returns the maximum value in the list

Returns the maximum value in the following list of numbers:

LEAST (expr1, expr2, expr3,…)

Returns the minimum value in the list

Returns the minimum value in the following list of numbers:

LN

Returns the natural logarithm of a number, based on e.

Returns the natural logarithm of 2:

LOG (x) or LOG (base, x)

Returns the natural logarithm (the base logarithm), and if you have the base parameter, base is the specified base.

SELECT LOG(20.085536923188) -- 3

LOG10 (x)

Returns the logarithm with a base of 10

SELECT LOG10(100) -- 2

LOG2 (x)

Returns the logarithm with a base of 2

Returns the logarithm of base 6 with 2:

SELECT LOG2(6);  -- 2.584962500721156

MAX (expression)

Returns the maximum value in the field expression

Returns the maximum value of the field Price in the data table Products:

SELECT MAX(Price) AS LargestPrice FROM Products;

MIN (expression)

Returns the minimum value in the field expression

Returns the minimum value of the field Price in the data table Products:

SELECT MIN(Price) AS MinPrice FROM Products;

MOD (XBI y)

Returns the remainder of x divided by y

The remainder of 5 divided by 2:

SELECT MOD(5,2) -- 1

PI ()

Returns pi (3.141593)

SELECT PI() --3.141593

POW (XBI y)

Returns x to the y power

2 to the third power:

SELECT POW(2,3) -- 8

POWER (XBI y)

Returns x to the y power

2 to the third power:

SELECT POWER(2,3) -- 8

RADIANS (x)

Convert angles to radians

Convert 180 degrees to radians:

SELECT RADIANS(180) -- 3.1415926535898

RAND ()

Returns a random number from 0 to 1

SELECT RAND() --0.93099315644334

ROUND (x [,y] )

Returns the integer closest to x. The optional parameter y represents the number of decimal places to be rounded. If omitted, the integer is returned.

SELECT ROUND(1.23456) --1
SELECT ROUND(345.156, 2) -- 345.16

SIGN (x)

Returns the symbol of x, where x is negative, 0, and positive returns-1, 0, and 1, respectively

SELECT SIGN(-10) -- (-1)

SIN (x)

Find the sine (the parameter is radians)

SELECT SIN(RADIANS(30)) -- 0.5

SQRT (x)

Returns the square root of x

The square root of 25:

SELECT SQRT(25) -- 5

SUM (expression)

Returns the sum of the specified fields

Calculate the sum of the fields Quantity in the OrderDetails table:

SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;

TAN (x)

Find the tangent (the parameter is radians)

SELECT TAN(1.75);  -- -5.52037992250933

TRUNCATE (XBI y)

Returns the value x to the y place after the decimal point (the biggest difference from ROUND is that it is not rounded)

SELECT TRUNCATE(1.23456,3) -- 1.234

MySQL date function

Function name

Description

Example

ADDDATE (dQuery n)

Calculate the start date d plus the date of n days

SELECT ADDDATE("2017-06-15", INTERVAL 10 DAY);
->2017-06-25

ADDTIME (tjinn)

N is a time expression, time t plus time expression n

Add 5 seconds:

SELECT ADDTIME('2011-11-11 11:11:11', 5);
->2011-11-11 11:11:16 ()

Add 2 hours, 10 minutes, 5 seconds:

SELECT ADDTIME("2020-06-15 09:34:21", "2:10:5"); 
-> 2020-06-15 11:44:26

CURRENT_DATE ()

Returns the current date

SELECT CURDATE();
-> 2018-09-19

CURRENT_TIME

Return to the current time

SELECT CURRENT_DATE();
-> 2018-09-19

CURRENT_TIMESTAMP ()

Returns the current date and time

SELECT CURRENT_TIME();
-> 19:59:02

CURTIME ()

Return to the current time

SELECT CURRENT_TIMESTAMP()
-> 2018-09-19 20:57:43

DATE ()

Extract a date value from a date or date-time expression

SELECT CURTIME();
-> 19:59:02

DATEDIFF (d1 and d2)

Calculate the number of days between date D1-> D2

SELECT DATE("2017-06-15");    
-> 2017-06-15

DATE_ADD (dCentInterval expr type)

After calculating the start date d plus a date after a time period, the type value can be:

  • MICROSECOND

  • SECOND

  • MINUTE

  • HOUR

  • DAY

  • WEEK

  • MONTH

  • QUARTER

  • YEAR

  • SECOND_MICROSECOND

  • MINUTE_MICROSECOND

  • MINUTE_SECOND

  • HOUR_MICROSECOND

  • HOUR_SECOND

  • HOUR_MINUTE

  • DAY_MICROSECOND

  • DAY_SECOND

  • DAY_MINUTE

  • DAY_HOUR

  • YEAR_MONTH

SELECT DATEDIFF('2001-01-01','2001-02-02')
-> -32

DATE_FORMAT (dline f)

Display the date d as required by the expression f

SELECT DATE_ADD("2017-06-15", INTERVAL 10 DAY);    
-> 2017-06-25

SELECT DATE_ADD("2017-06-15 09:34:21", INTERVAL 15 MINUTE);
-> 2017-06-15 09:49:21

SELECT DATE_ADD("2017-06-15 09:34:21", INTERVAL -3 HOUR);
->2017-06-15 06:34:21

SELECT DATE_ADD("2017-06-15 09:34:21", INTERVAL -3 MONTH);
->2017-04-15

DATE_SUB (date,INTERVAL expr type)

Function subtracts the specified interval from the date.

SELECT DATE_FORMAT('2011-11-11 11:11:11','%Y-%m-%d %r')
-> 2011-11-11 11:11:11 AM

DAY (d)

Returns the date portion of the date value d

The OrderDate field in the Orders table minus 2 days:

SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 2 DAY) AS OrderPayDate
FROM Orders

DAYNAME (d)

Return date d is the day of the week, such as Monday,Tuesday

SELECT DAY("2017-06-15");  
-> 15

DAYOFMONTH (d)

The calculated date d is the day of this month.

SELECT DAYNAME('2011-11-11 11:11:11')
->Friday

DAYOFWEEK (d)

Date d what day is today, 1 Sunday, 2 Monday, and so on

SELECT DAYOFMONTH('2011-11-11 11:11:11')
->11

DAYOFYEAR (d)

The calculated date d is the day of this year.

SELECT DAYOFWEEK('2011-11-11 11:11:11')
->6
SELECT DAYOFYEAR('2011-11-11 11:11:11')
->315

EXTRACT (type FROM d)

Gets the specified value from date d, and type specifies the returned value.

  • MICROSECOND

  • SECOND

  • MINUTE

  • HOUR

  • DAY

  • WEEK

  • MONTH

  • QUARTER

  • YEAR

  • SECOND_MICROSECOND

  • MINUTE_MICROSECOND

  • MINUTE_SECOND

  • HOUR_MICROSECOND

  • HOUR_SECOND

  • HOUR_MINUTE

  • DAY_MICROSECOND

  • DAY_SECOND

  • DAY_MINUTE

  • DAY_HOUR

  • YEAR_MONTH

SELECT EXTRACT(MINUTE FROM '2011-11-11 11:11:11') 
-> 11

FROM_DAYS (n)

Calculate the date n days after January 1, 2000

SELECT FROM_DAYS(1111)
-> 0003-01-16

HOUR (t)

Returns the hourly value in t

SELECT HOUR('1:2:3')
-> 1

LAST_DAY (d)

Returns the last day of January to a given date

SELECT LAST_DAY("2017-06-20");
-> 2017-06-30

LOCALTIME ()

Returns the current date and time

SELECT LOCALTIME()
-> 2018-09-19 20:57:43

LOCALTIMESTAMP ()

Returns the current date and time

SELECT LOCALTIMESTAMP()
-> 2018-09-19 20:57:43

MAKEDATE (year, day-of-year)

Returns a date based on the given parameter year year and the number of days in the year day-of-year

SELECT MAKEDATE(2017, 3);
-> 2017-01-03

MAKETIME (hour, minute, second)

Combination time. Parameters are hours, minutes and seconds, respectively.

SELECT MAKETIME(11, 35, 4);
-> 11:35:04

MICROSECOND (date)

Returns the number of microseconds corresponding to the date parameter

SELECT MICROSECOND("2017-06-20 09:34:00.000023");
-> 23

MINUTE (t)

Returns the minute value in t

SELECT MINUTE('1:2:3')
-> 2

MONTHNAME (d)

Returns the month name among the dates, such as November

SELECT MONTHNAME('2011-11-11 11:11:11')
-> November

MONTH (d)

Returns the month value in date d, 1 to 12

SELECT MONTH('2011-11-11 11:11:11')
->11

NOW ()

Returns the current date and time

SELECT NOW()
-> 2018-09-19 20:57:43

PERIOD_ADD (period, number)

Add a period for the year-month combination date

SELECT PERIOD_ADD(201703, 5);   
-> 201708

PERIOD_DIFF (period1, period2)

Returns the month difference between two periods

SELECT PERIOD_DIFF(201710, 201703);
-> 7

QUARTER (d)

Return date d is the season, return 1 to 4

SELECT QUARTER('2011-11-11 11:11:11')
-> 4

SECOND (t)

Returns the second value in t

SELECT SECOND('1:2:3')
-> 3

SEC_TO_TIME (s)

Convert time s in seconds to hours and seconds

SELECT SEC_TO_TIME(4320)
-> 01:12:00

STR_TO_DATE (string, format_mask)

Convert a string to a date

SELECT STR_TO_DATE("August 10 2017", "%M %d %Y");
-> 2017-08-10

SUBDATE (dQuery n)

Date d minus n days later

SELECT SUBDATE('2011-11-11 11:11:11', 1)
->2011-11-10 11:11:11 (默认是天)

SUBTIME (tjinn)

Time t minus n seconds

SELECT SUBTIME('2011-11-11 11:11:11', 5)
->2011-11-11 11:11:06 ()

SYSDATE ()

Returns the current date and time

SELECT SYSDATE()
-> 2018-09-19 20:57:43

TIME (expression)

Extract the time portion of the incoming expression

SELECT TIME("19:30:10");
-> 19:30:10

TIME_FORMAT (tmenf)

Display the time t as required by the expression f

SELECT TIME_FORMAT('11:11:11','%r')
11:11:11 AM

TIME_TO_SEC (t)

Convert time t to seconds

SELECT TIME_TO_SEC('1:12:00')
-> 4320

TIMEDIFF (time1, time2)

Calculate the time difference

mysql> SELECT TIMEDIFF("13:10:11", "13:10:10");
-> 00:00:01
mysql> SELECT TIMEDIFF('2000:01:01 00:00:00',
->                 '2000:01:01 00:00:00.000001');
-> '-00:00:00.000001'
mysql> SELECT TIMEDIFF('2008-12-31 23:59:59.000001',
->                 '2008-12-30 01:01:01.000002');
-> '46:58:57.999999'

TIMESTAMP (expression, interval)

When there is a single parameter, the function returns a date or date-time expression; when there are two parameters, the parameters are added.

mysql> SELECT TIMESTAMP("2017-07-23",  "13:10:11");
-> 2017-07-23 13:10:11
mysql> SELECT TIMESTAMP('2003-12-31');
-> '2003-12-31 00:00:00'
mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
-> '2004-01-01 00:00:00'

TIMESTAMPDIFF (unit,datetime_expr1,datetime_expr2)

Calculates the time difference and returns the time difference of datetime_expr2 − datetime_expr1

mysql> SELECT TIMESTAMPDIFF(DAY,'2003-02-01','2003-05-01');   // 计算两个时间相隔多少天
-> 89
mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');   // 计算两个时间相隔多少月
-> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');    // 计算两个时间相隔多少年
-> -1
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');  // 计算两个时间相隔多少分钟
-> 128885

TO_DAYS (d)

Calculate the number of days from date d to January 1, 2000

SELECT TO_DAYS('0001-01-01 01:01:01')
-> 366

WEEK (d)

The calculated date d is the week ordinal of this year, ranging from 0 to 53

SELECT WEEK('2011-11-11 11:11:11')
-> 45

WEEKDAY (d)

The date d is what day of the week, 0 means Monday, 1 means Tuesday

SELECT WEEKDAY("2017-06-15");
-> 3

WEEKOFYEAR (d)

The calculated date d is the week ordinal of this year, ranging from 0 to 53

SELECT WEEKOFYEAR('2011-11-11 11:11:11')
-> 45

YEAR (d)

Returns the year

SELECT YEAR("2017-06-15");
-> 2017

YEARWEEK (date, mode)

Returns the year and week (0 to 53). In mode, 0 represents Sunday, 1 represents Monday, and so on.

SELECT YEARWEEK("2017-06-15");
-> 201724

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.