April 1 SQL Common functions

Source: Internet
Author: User
Tags float number rand

One, the string function

1, ASCII (' string ')--Returns the ASCII code value of the leftmost character of the string

Select ASCII (' a ')--Returns 97

Select ASCII (' ABCD ')--also returns 97

2, CHAR (' Integer ')--converts ASCII value to character, integer range 0-255

Select CHAR (' 97 ')--return a

3, LEN ()--Returns the length of the given string, without trailing spaces

Select LEN (' AASSDD ')--return 6
Select LEN (' AASSDD ')--return 8
Select LEN (' AASSDD ')--return 8

4. Left--intercept string

Left (' string ', number of digits)

Select Left (' ABCDEFG ', 3)--Return to ABC

Right--intercept string
Select Right (' ABCDEFG ', 3)--back to EFG

5, Lower and upper

Select LOWER (' AbCdEfG ')--return to AbCdEfG
Select UPPER (' AbCdEfG ')--return to AbCdEfG

Second, date and time functions

1, DATEADD--add a period of time to the specified date to return a new datetime value

DATEADD (time interval type (year, month, day), Interval time (integer), ' Time Date ')

Example: Select DATEADD (month,2, ' 1993-08-28 ')--return to 1993-10-28

2, DATEDIFF--Find the difference between two dates

Format: DATEDIFF (time interval type (year, month, day), ' Start time ', ' End Time ')

Select DATEDIFF (year, ' 1990-03-02 ', ' 1997-12-08 ')--return 7
Select DATEDIFF (MONTH, ' 1990-03-02 ', ' 1997-12-08 ')--return 93
Select DATEDIFF (Day, ' 1990-03-02 ', ' 1997-12-08 ')--return 2838

3, day, month, year--the date of the month of removal

Format: Day (data), MONTH (data), year (data)

Select Day (' 1987-09-18 ')--return 18
Select MONTH (' 1987-09-18 ')--return 9
Select year (' 1987-09-18 ')--return 1987

4, GETDATE--Take the current system time

Select GETDATE ()--return current time 2015-04-01 11:01:30.687

Third, numerical functions

1. CEILING Ceiling Number (returns the smallest integer greater than or equal to the given number)

Floor number (returns the largest integer less than or equal to the given number)

Select CEILING (5.4)--Return 6
Select Floor (5.4)--Return 5

2. RAND

Select RAND ()--generates a random float number between 0-1

3. POWER--return

Select POWER (3,2)--Return 9
Select POWER (2.15,3)--Return 9.94

4. Convert-type conversion (cast ... As is also a type conversion)

Example: birthday=1997-8-28

CAST (Year (BIRTHDAY) as varchar (10) + ' Years ' +mouth (BIRTHDAY) as varchar (10) + ' month ' +day (BIRTHDAY) as varchar (10) + ' Day ')

Date changed after execution: August 28, 1997

April 1 SQL Common functions

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.