SQL SUBSTRING functions

Source: Internet
Author: User

SUBSTRING (expression, start, length)

Parameters
expression
A string, binary string, text, image, column, or an expression that contains a column. Do not use an expression that contains an aggregate function.

Start
An integer or an expression that can be implicitly converted to int, specifying the starting position of the substring, starting at 1.

length
An integer or an expression that can be implicitly converted to an int, specifying the length of the substring. After testing, the discovery can only be non-negative .

return value

1. If expression is a supported binary data type, then binary data is returned, which we will not discuss for the moment.
2. If expression is a supported character data type, the character data is returned.

(1) If the index of start starts at 1, the string intercept starts at the first character of the expression, starting with the second character of the expression from 2, and so on.

For example:

Select SUBSTRING (' ABCDE ', up) returns the result AB

Select SUBSTRING (' ABCDE ', 2, 3) returns the result BCD

Select SUBSTRING (' ABCDE ', 1,0) returns the null result

Select SUBSTRING (' ABCDE ', 0,8) returns the result as ABCDE, noting that there are no spaces behind it.

(2) if the index of start starts at 1 (0 or negative ), the return length is equal to 1, the intercept length is the absolute value (start-1), and if the difference is negative, the return is empty .

Draw a diagram to understand this situation (e.g. substring (' ABCDE ', 0,2) returns a):

Coordinates: 0 1 2 3 4 5
Content: null a b c d E
Intercept: intercept from here to end

For example: the following | | Represents an absolute value calculation

Select SUBSTRING (' ABCDE ', 0,2) returns the result as a, the formula is SUBSTRING (1,2-|2-1|)

Select SUBSTRING (' ABCDE ', 0,-1) returns an error "invalid length parameter passed to SUBSTRING function"

Select SUBSTRING (' ABCDE ', -1,2) returns a null result with a formula of SUBSTRING (1,2-|-1-1|)

Select SUBSTRING (' ABCDE ', -5,3) returns a null result with a formula of SUBSTRING (1,3-|-5-1|)

Select SUBSTRING (' ABCDE ', -1,4) returns the result of AB, calculated as SUBSTRING (1,4-|-1-1|)

Select SUBSTRING (' ABCDE ', -2,8) returns the result as ABCDE, calculated as SUBSTRING (1,8-|-2-1|)

SQL SUBSTRING 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.