Summary of SQL functions

SQL contains the following seven types of functions:

One, aggregate function

  1. Aggregate function: returns a summary value  (it returns a value for each row set applied to it)
    1. AVG(表达式) Return all of the expressions 平均值. Only used for numeric columns and automatically ignore NULL values.
    2. COUNT(表达式) Return to the expression 非NULL值的数量. Can be used for numeric and character columns.
    3. COUNT(*) Returns in the table 行数( including columns with NULL values ).
    4. MAX(表达式) Returns in the expression 最大值, ignoring NULL values. Can be used for numbers, characters, and date and time columns.
    5. MIN(表达式) Returns in the expression 最小值, ignoring NULL values. Can be used for numbers, characters, and date and time columns.
    6. SUM(表达式) Return all the expressions 总和, ignoring NULL values. Only used for numeric columns.

Second, the conversion function

  1. Transformation function: convert one data type to another ( convert and cast)
    1. CONVERT(data_type[(length)], expression [, style])
      1. 例: Select convert(varchar(10) ,stuno) as stuno,stuname from student
    2. CAST( expression AS data_type )
      1. 例: Select cast(stuno as varchar(10)) as stuno,stuname from student

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

  1. Date function: processing date and time (because you can't directly execute arithmetic functions, date functions are very useful)
    1. GETDATE() The current system date.
      1. Example: select GETDATE() --Result: 2020-10-19 15:34:27.343
    2. DATEADD(日期部分,number,date) Returns the date (date) with the specified number (number), which is added to the specified date part (datepart)
      1. Example: elect DATEADD(dd, 5, getdate()) --Add 5 days
    3. DATEDIFF(日期部分,date1,date2) Return the specified in two dates日期部分之间的差值
      1. Example: select DATEDIFF(mm, '2010-1-1', '2010-3-1 00:00:00') --Result: 2
    4. DATENAME(日期部分,date) Returns the date part of the date字符串形式
      1. Example: select DATENAME(dw,GETDATE()) --Result: Tuesday
    5. DATEPART(日期部分,date) Returns the date part of the specified date整数形式
      1. 例:select DATEPART(dw,GETDATE()) --Result (returns today is the day of the week): 3
    6. YEAR(date) Returns the 年份value of the specified date
      1. Example: select YEAR(GETDATE()) --Result: 2020
    7. MONTH(date)Returns the 月份value of the specified date
    8. DAY(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 qq 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

  1. Mathematical functions: perform arithmetic operations (perform algebraic operations on digital values)
    1. ABS(num_expr) Returns the absolute value of a numeric expression.

    2. ACOS(float_expr) Returns the angle (expressed in radians) whose cosine is similar to the specified floating-point expression.

    3. ASIN(float_expr) Returns the angle (expressed in radians), whose sine is similar to the specified floating-point expression.

    4. ATAN(float_expr) Returns the angle (expressed in radians), whose tangent is similar to the specified floating-point expression.

    5. ATN2(float_expr1, float_expr2) Returns the angle (in radians) whose tangent is between two approximate floating-point expressions.

    6. CEILING(num_expr) Returns the smallest integer greater than or equal to the numeric expression.

    7. COS(float_expr) Returns the value of the cosine trigonometric function expressed as a floating-point expression that approximates the specified angle (in radians).

    8. COT(float_expr) Returns the value of the cotangent trigonometric function expressed in a floating-point expression that approximates the specified angle (in radians).

    9. DEGREES(num_expr)Returns the degree value corresponding to the radian value represented by the numeric expression.

    10. EXP(float_expr) According to the specified approximate floating-point expression, return the exponent value.

    11. FLOOR(num_expr) Returns the largest integer less than or equal to a numeric expression

    12. LOG(float_expr) According to the specified approximate floating-point expression, return the natural logarithmic value.

    13. LOG10(float_expr) According to the specified approximate floating-point expression, return the logarithm to the bottom.

    14. PI() Returns the constant value. 141592653589793

    15. POWER(num_expr,y) Returns the value of a numeric expression whose power is y.

    16. RADIANS(num_expr) Returns the radian value corresponding to the degree value represented by the numeric expression.

    17. RAND([seed]) The approximate floating-point value between randomly returned to and can be specified as an integer expression for seed (optional).

    18. ROUND(num_expr,length) Intercept the specified integer length for the numeric expression, and return the rounded value.

    19. SIGN(num_expr) Perform +1 operation on positive numbers, and -1 operation on negative numbers and zero.

    20. SIN(float_expr) Returns the value of the sine trigonometric function expressed in a floating-point expression that approximates the specified angle (in radians).

    21. SQUARE(float_expr) Returns the average value of floating-point expressions.

    22. SQRT(float_expr) Returns the square root of the specified approximate floating-point expression.

    23. 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

  1. 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)
    1. Expr1+expr2 Returns the string of the combined form of the two expressions.

    2. ASCII(char_expr) Returns the ASCⅡ code value of the leftmost character of the expression.

    3. 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.

    4. CHARINDEX('pattern',char_expr) Returns the starting position of the specified pattern in the character expression.

    5. DIFFERENCE(char_expr1,char_expr2) According to comparing the similarity of two character expressions, return to the value between. Indicates the best match.

    6. LEN(char_expr) Returns the length of the character expression.

    7. LOWER(char_expr) Convert all character expressions to lowercase.

    8. LTRIM(char_expr) Returns the character expression with the preceding spaces removed.

    9. 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.

    10. REPLICATE(char_expr,int_expr) Returns the character string produced by repeating the specified number of character expressions.

    11. REVERSE(char_expr) Reverse character expression.

    12. RIGHT(char_expr,int_expr) Returns the characters from the right end of the character expression according to the specified number of characters.

    13. RTRIM(char_expr) Returns a character expression with spaces after it removed.

    14. SOUNDEX(char_expr) The bit code obtained after evaluating the similarity of two strings.

    15. SPACE(int_expr) Returns a string containing the specified number of spaces.

    16. STR(float_expr[,length[,decimal]]) Returns the string representation of the floating-point expression.

    17. 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.

    18. SUBSTRING(char_expr,start,length) Returns the character set obtained by intercepting the specified length from the specified position of the character expression.

    19. UPPER(char_expr) Convert all character expressions to uppercase.

Six, system functions

  1. System function: return from the database the special information of the value, object or setting in SQLSERVER (used to return metadata or configuration settings)
    1. COALESCE(expr1,expr2, xprN) Returns the first non-NULL expression.

    2. COL_LENGTH('table_name','column_name') Returns the length of the column.

    3. COL_NAME(table_id,column_id) Returns the name of the column in the specified table.

    4. DATALENGTH('expr') Returns the actual length of any data type.

    5. DB_ID([‘database_name']) Returns the identification number of the database.

    6. DB_NAME([database_id]) Returns the name of the database.

    7. GETANSINULL([‘database_name']) Returns the default Nullability of the database.

    8. HOST_ID() Returns the identification number of the workstation.

    9. HOST_NAME() Returns the name of the workstation.

    10. IDENT_INCR('table_or_view') When a new record is added to the table, the count increases.

    11. IDENT_SEED('table_or_view') Returns the starting number of the identity column.

    12. INDEX_COL('table_name',index_id,key_id) Returns the column name of the index.

    13. ISNULL(expr,value) NULL expression replaced with the specified value.

    14. NULLIF(expr1,expr2) When Expr1 and Expr2 are equal, Null is returned.

    15. OBJECT_ID('obj_name') Returns the database object identification number.

    16. OBJECT_NAME('object_id') Returns the name of the database object.

    17. STATS_DATE(table_id,index_id) Returns the date of the last update of statistics for the specified index.

    18. SUSER_SID([‘login_name']) Returns the user's login identification number.

    19. SUSER_ID([‘login_name']) Returns the user's login identification number. This function is similar to the SUSER_SID() function and retains backward compatibility.

    20. SUSER_SNAME([server_user_id]) Returns the user's login identification number.

    21. 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.

    22. USER_ID('user_name') Returns the user's database identification number.

    23. USER_NAME(['user_id']) Returns the user's database name.

Seven, text and image functions

  1. 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)
    1. 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.

    2. TEXTVALID('table_name.col_name',text_ptr)Check whether the given text pointer is valid. Return means valid, return means pointer invalid.

Guess you like

Origin blog.csdn.net/qq_37192571/article/details/109163244
Recommended