📜  MySQL 中的各种字符串、数字和日期和时间函数

📅  最后修改于: 2022-05-13 01:54:56.590000             🧑  作者: Mango

MySQL 中的各种字符串、数字和日期和时间函数

函数是一种特殊类型的预定义命令集,它执行某些操作并返回单个值。函数对提供给它们的零个、一个、两个或多个值进行操作。提供给函数的值称为参数或自变量。

MySQL 函数已被分类为各种类别,例如字符串函数、数学函数、日期和时间函数等。尽管 MySQL 提供了大量的函数,但在本次讨论中,我们将仅限于某些某些函数。

1. 字符串函数:
MySQL 的字符串函数可以通过多种方式操作文本字符串。下面讨论一些常用的字符串函数:

S.No.FunctionDescriptionExamples
1.CHAR()Returns the character for each integer passes

1. SELECT CHAR(70, 65, 67, 69) ;

2. SELECT CHAR(65, 67.3, 69.3) ;

2.CONCAT()Returns concatenated string

SELECT CONCAT(name, aggregate) AS “Name Marks”

FROM student WHERE age = 14 OR age = 16;

3.LOWER() /LCASE()Returns the argument in lowercase

SELECT LOWER(‘GEEKSFORGEEKS’) AS “LowerName1”,

LOWER(‘Geeks For Geeks’) AS “LowerName2” ;

4.SUBSTRING(), SUBSTR()Returns the substring as specified

1. SELECT SUBSTR(‘ABSDEFG’, 3, 4) “Subs” ;

2. SELECT SUBSTR(‘ABCDEFG’, -5, 4) “Subs” ;

5.UPPER()/UCASE()Converts to uppercase

SELECT UPPER(‘Large’) “Uppercase” ;

or SELECT UCASE(‘Large’) “Uppercase”;

6.TRIM()Removes leading and trailing spacesSELECT TRIM(‘Bar One’) ;
7.LENGTH()Returns the length of a string in bytesSELECT LENGTH(‘CANDIDE’) “Length in characters” ;

2. 数字函数:
数字函数是那些接受数字值并在执行所需操作后返回数字值的函数。下面正在讨论一些有用的数字函数:

S.No.FunctionDescriptionExample
1.MOD()Returns the remainder of one expression by diving y another expression.SELECT MOD(11, 4) “Modulus” ;
2.POWER()/POW()Returns the value of one expression raised to the power of another expressionSELECT POWER(3, 2) “Raised” ;
3.ROUND()Returns numeric expression rounded to an integer. Can be used to round an expression to a number of decimal points.SELECT ROUND(15.193, 1) “Round” ;
4.SIGN()This function returns sign of a given number.SELECT SIGN(-15) “Sign” ;
5.SQRT()Returns the non-negative square root of numeric expression.SELECT SQRT(26) “Square root” ;
6.TRUNCATE()Returns numeric exp1 truncate to exp2 decimal places. If exp2 is 0, then the result will have no decimal pointDRLRCT TRUNCATE(15.79, 1) “Truncate” ;

3. 日期和时间函数:
日期函数对 DATE 数据类型的值进行操作。

S.No.FunctionDescriptionExample
1

CURDATE()/

CURRENT_DATE()/

CURRENT_DATE

Returns the current date.SELECT CURDATE() ;
2DATE()Extracts the date part of a date or date-time expression.SELECT DATE(‘2020-12-31 01:02:03’) ;
3MONTH()Returns the month from the date passed.SELECT MONTH(‘2020-12-31’) ;
4YEAR()Returns the yearSELECT YEAR(‘2020-12-31’) ;
5NOW()Returns the time at which the function executes.SELECT NOW() ;
6SYSDATE()Returns the current date and time.

SELECT NOW(), SLEEP(2), NOW() ;

or SELECT SYSDATE(), SLEEP(2), SYSDATE() ;