SQL Server substring (expression, start, length) Function

Source: Internet
Author: User

Syntax for SUBSTRING:  SUBSTRING(expression, start, length)
Expression
String, binary string, text, image, column, or expression that contains a column. Do not use expressions that contain aggregate functions.
Start
An integer or expression that can be implicitly converted to an int indicates the start position of the substring.
Length

An integer or expression that can be implicitly converted to an int value refers to the length of the stator string.

Example:

1. The starting position is 0 and the truncation length is 3.

select substring(DWMC,0,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'
Result:

2. The starting position is 1 and the truncation length is 3.

select substring(DWMC,1,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'
Result:

3. The starting position is-1 and the truncation length is 3.

select substring(DWMC,-1,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'
Result:


4. The starting position is-2 and the truncation length is 3.

select substring(DWMC,-2,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'
Result:

5. The starting position is-9, and the truncation length is 3.

select substring(DWMC,-9,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'

Result:

Through the above example, we can see that the truncation of the substring is similar to the absolute length on the X coordinate. For the string, the position of negative number and zero is empty, there are actually several locations starting from 1!



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.