SQL contains the following seven types of functions:
One, aggregate function
- Aggregate function: returns a summary value (it returns a value for each row set applied to it)
AVG(表达式)
Return all of the expressions平均值
. Only used for numeric columns and automatically ignore NULL values.COUNT(表达式)
Return to the expression非NULL值的数量
. Can be used for numeric and character columns.COUNT(*)
Returns in the table行数
( including columns with NULL values ).MAX(表达式)
Returns in the expression最大值
, ignoring NULL values. Can be used for numbers, characters, and date and time columns.MIN(表达式)
Returns in the expression最小值
, ignoring NULL values. Can be used for numbers, characters, and date and time columns.SUM(表达式)
Return all the expressions总和
, ignoring NULL values. Only used for numeric columns.
Second, the conversion function
- Transformation function: convert one data type to another ( convert and cast)
- CONVERT(data_type[(length)], expression [, style])
- 例: Select convert(varchar(10) ,stuno) as stuno,stuname from student
- CAST( expression AS data_type )
- 例: Select cast(stuno as varchar(10)) as stuno,stuname from student
- CONVERT(data_type[(length)], expression [, style])
The difference between Cast and Convert
Both Cast and Convert are used to convert an expression of one data type to an expression of another data type. CAST and CONVERT provide similar functions, but the syntax is different. Convert is generally used in time conversion, because it adds a style more than cast, which can be converted into different time formats.
Three, date function
- Date function: processing date and time (because you can't directly execute arithmetic functions, date functions are very useful)
GETDATE()
The current system date.- Example: select GETDATE() --Result: 2020-10-19 15:34:27.343
DATEADD(日期部分,number,date)
Returns the date (date) with the specified number (number), which is added to the specified date part (datepart)- Example: elect DATEADD(dd, 5, getdate()) --Add 5 days
DATEDIFF(日期部分,date1,date2)
Return the specified in two dates日期部分之间的差值
- Example: select DATEDIFF(mm, '2010-1-1', '2010-3-1 00:00:00') --Result: 2
DATENAME(日期部分,date)
Returns the date part of the date字符串形式
- Example: select DATENAME(dw,GETDATE()) --Result: Tuesday
DATEPART(日期部分,date)
Returns the date part of the specified date整数形式
例:
select DATEPART(dw,GETDATE()) --Result (returns today is the day of the week): 3
YEAR(date)
Returns the年份
value of the specified date- Example: select YEAR(GETDATE()) --Result: 2020
MONTH(date)
Returns the月份
value of the specified dateDAY(date)
Returns the天数
value of the specified date
Note :
-
The difference between DATENAME and DATEPART, the return value type is different, one is VARCHAR and the other is INT, and the other is that the week will be expressed in the local language
-
When displaying the content of the date column, if only the year, month, and day are displayed, you can use the CONVERT conversion function to convert the date column
CONVERT(VARCHAR(10), date field name, 120) --120 is the date format YYYY-MM-DD
SELECT CONVERT(VARCHAR(10), inventory date, 120) AS inventory date FROM raw material inventory date schedule
The representation method and valid range of the date in SQL are as follows:
Date part | abbreviation | value | Date part | abbreviation | value |
year | yy | 1753-9999 | week | wk | 1-53 |
Quarterly | 1-4 | hour | hh | 0-23 | |
month | mm | 1-12 | minute | me | 0-59 |
Day of the year | two | 1-366 | second | ss | 0-59 |
Day of the month | dd | 1-31 | millisecond | ms | 0-999 |
Day of the week | dw | 1-7 |
Four, mathematical functions
- Mathematical functions: perform arithmetic operations (perform algebraic operations on digital values)
-
ABS(num_expr)
Returns the absolute value of a numeric expression. -
ACOS(float_expr)
Returns the angle (expressed in radians) whose cosine is similar to the specified floating-point expression. -
ASIN(float_expr)
Returns the angle (expressed in radians), whose sine is similar to the specified floating-point expression. -
ATAN(float_expr)
Returns the angle (expressed in radians), whose tangent is similar to the specified floating-point expression. -
ATN2(float_expr1, float_expr2)
Returns the angle (in radians) whose tangent is between two approximate floating-point expressions. -
CEILING(num_expr)
Returns the smallest integer greater than or equal to the numeric expression. -
COS(float_expr)
Returns the value of the cosine trigonometric function expressed as a floating-point expression that approximates the specified angle (in radians). -
COT(float_expr)
Returns the value of the cotangent trigonometric function expressed in a floating-point expression that approximates the specified angle (in radians). -
DEGREES(num_expr)
Returns the degree value corresponding to the radian value represented by the numeric expression. -
EXP(float_expr)
According to the specified approximate floating-point expression, return the exponent value. -
FLOOR(num_expr)
Returns the largest integer less than or equal to a numeric expression -
LOG(float_expr)
According to the specified approximate floating-point expression, return the natural logarithmic value. -
LOG10(float_expr)
According to the specified approximate floating-point expression, return the logarithm to the bottom. -
PI()
Returns the constant value. 141592653589793 -
POWER(num_expr,y)
Returns the value of a numeric expression whose power is y. -
RADIANS(num_expr)
Returns the radian value corresponding to the degree value represented by the numeric expression. -
RAND([seed])
The approximate floating-point value between randomly returned to and can be specified as an integer expression for seed (optional). -
ROUND(num_expr,length)
Intercept the specified integer length for the numeric expression, and return the rounded value. -
SIGN(num_expr)
Perform +1 operation on positive numbers, and -1 operation on negative numbers and zero. -
SIN(float_expr)
Returns the value of the sine trigonometric function expressed in a floating-point expression that approximates the specified angle (in radians). -
SQUARE(float_expr)
Returns the average value of floating-point expressions. -
SQRT(float_expr)
Returns the square root of the specified approximate floating-point expression. -
TAN(float_expr)
Returns the value of the tangent trigonometric function expressed in a floating-point expression that approximates the specified angle (expressed in radians).
-
Five, string functions
- String functions: perform operations on strings, binary data or expressions (can be used for binary and varbinary data type columns, but mainly for char and varchar data types)
-
Expr1+expr2
Returns the string of the combined form of the two expressions. -
ASCII(char_expr)
Returns the ASCⅡ code value of the leftmost character of the expression. -
CHAR(int_expr)
Return to the ASCⅡ character value of the integer expression between. If the entered value is not in the valid range, NULL is returned. -
CHARINDEX('pattern',char_expr)
Returns the starting position of the specified pattern in the character expression. -
DIFFERENCE(char_expr1,char_expr2)
According to comparing the similarity of two character expressions, return to the value between. Indicates the best match. -
LEN(char_expr)
Returns the length of the character expression. -
LOWER(char_expr)
Convert all character expressions to lowercase. -
LTRIM(char_expr)
Returns the character expression with the preceding spaces removed. -
PATINDEX('%pattern%',expr)
Returns the starting position of the first occurrence of the pattern in the expression. Return indicates that there is no pattern form. -
REPLICATE(char_expr,int_expr)
Returns the character string produced by repeating the specified number of character expressions. -
REVERSE(char_expr)
Reverse character expression. -
RIGHT(char_expr,int_expr)
Returns the characters from the right end of the character expression according to the specified number of characters. -
RTRIM(char_expr)
Returns a character expression with spaces after it removed. -
SOUNDEX(char_expr)
The bit code obtained after evaluating the similarity of two strings. -
SPACE(int_expr)
Returns a string containing the specified number of spaces. -
STR(float_expr[,length[,decimal]])
Returns the string representation of the floating-point expression. -
STUFF(char_expr1,start,length,char_expr2)
Use the character expression to replace a part of the character expression, starting from the specified position to replace the specified length. -
SUBSTRING(char_expr,start,length)
Returns the character set obtained by intercepting the specified length from the specified position of the character expression. -
UPPER(char_expr)
Convert all character expressions to uppercase.
-
Six, system functions
- System function: return from the database the special information of the value, object or setting in SQLSERVER (used to return metadata or configuration settings)
-
COALESCE(expr1,expr2, xprN)
Returns the first non-NULL expression. -
COL_LENGTH('table_name','column_name')
Returns the length of the column. -
COL_NAME(table_id,column_id)
Returns the name of the column in the specified table. -
DATALENGTH('expr')
Returns the actual length of any data type. -
DB_ID([‘database_name'])
Returns the identification number of the database. -
DB_NAME([database_id])
Returns the name of the database. -
GETANSINULL([‘database_name'])
Returns the default Nullability of the database. -
HOST_ID()
Returns the identification number of the workstation. -
HOST_NAME()
Returns the name of the workstation. -
IDENT_INCR('table_or_view')
When a new record is added to the table, the count increases. -
IDENT_SEED('table_or_view')
Returns the starting number of the identity column. -
INDEX_COL('table_name',index_id,key_id)
Returns the column name of the index. -
ISNULL(expr,value)
NULL expression replaced with the specified value. -
NULLIF(expr1,expr2)
When Expr1 and Expr2 are equal, Null is returned. -
OBJECT_ID('obj_name')
Returns the database object identification number. -
OBJECT_NAME('object_id')
Returns the name of the database object. -
STATS_DATE(table_id,index_id)
Returns the date of the last update of statistics for the specified index. -
SUSER_SID([‘login_name'])
Returns the user's login identification number. -
SUSER_ID([‘login_name'])
Returns the user's login identification number. This function is similar to the SUSER_SID() function and retains backward compatibility. -
SUSER_SNAME([server_user_id])
Returns the user's login identification number. -
SUSER_NAME([server_user_id])
Returns the user's login identification number. This function is similar to the SUSER_SNAME() function and retains backward compatibility. -
USER_ID('user_name')
Returns the user's database identification number. -
USER_NAME(['user_id'])
Returns the user's database name.
-
Seven, text and image functions
- Text and image functions: perform operations on text and image data (usually return the required information about text and image data. Text and image data are stored in a binary format)
-
TEXTPTR(col_name)
Returns the text pointer value in varbinary format. Check the text pointer to make sure it points to the first text page. -
TEXTVALID('table_name.col_name',text_ptr)
Check whether the given text pointer is valid. Return means valid, return means pointer invalid.
-