Dark horse programmer-common SQL functions _ MySQL

Source: Internet
Author: User
Tags case statement switch case
Dark horse programmer-common SQL functions bitsCN.com

Dark horse programmer-common SQL functions

Lists some common SQL functions.

Numeric functions

ABS () calculates the absolute value (reminds me of the ABS anti-lock system)

CEILING () is rounded to the maximum integer, and-3.6 is rounded to-3.

FLOOR () rounded to the smallest integer,-3.6 rounded to-4

ROUND () rounding, ROUND (3.141, 2) needs to input two parameters, the first is the operand, the last is the precision

String functions

LEN () calculates the string length

LOWER () to lowercase characters

Convert UPPER () to uppercase characters

LTRIM () removes the left space

RTRIM () remove right Space

SUBSTRING (string, start_position, lenth) string truncation function. the truncation length starting from start_position is lenth.

Date functions

GETDATE () obtains the current date.

The DATEADD (datepart, number, date) function is used to calculate the incremental date. datepart is the unit of measurement, and date is the date to be operated.

Datepart can be selected: year, quarter, month, dayofyear, day, week, weekday, hour, minute, second

DATEDIFF (datepart, startdate, enddate) returns the difference between two dates based on datepart

DATEPART (datepart, date) returns the specified part of the date.

Type conversion

CAST (expression as type)

CONVERT (type, expression)

Null value processing function

ISNULL (expression, value) determines whether expression is returned if expression is not empty; otherwise, value is returned.

Select ISNULL (name, 'Alias name') from Person

Single value judgment

Similar to the switch case statement.

CASE expression

WHEN value1 THEN return1

WHEN value2 THEN return2

ELSE return3

END

When the range is determined after the when, there can be no expression after the case.

ROW_NUMBER () function

The function is to count the row number.

ROW_NUMBER () is a window function. it cannot appear in where and can only appear in select or order.

Select * from

(Select ROW_NUMBER () OVER (order by salary) as rownum, id, name, from Person) as e1

Where e1.rownum> 3 and e1.rownum <5

In this way, you can obtain the desired data row at will.

-The End-

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.