SubString and CharIndex functions in SqlServer use SubString and CharInd

Source: Internet
Author: User
1. CharIndex1: CharIndex Syntax: CharIndex (expression1, expression2 [, start_location]) 2: an expression of the parameter expression1, which contains the sequence of characters to be searched. Expression1 is a string data expression. Expression2 is an expression, usually a column for the specified sequence search.

1. CharIndex 1: CharIndex Syntax: CharIndex (expression1, expression2 [, start_location]) 2: an expression of the parameter expression1, which contains the sequence of characters to be searched. Expression1 is a string data expression. Expression2 is an expression, usually a column for the specified sequence search.

I. CharIndex

1: CharIndex Syntax:

CharIndex (expression1, expression2 [, start_location])

2: Parameters

Expression1

An expression that contains the sequence of characters to be searched. Expression1 is a string data expression.

Expression2

An expression is usually a column for the specified sequence search. Expression2 belongs to the string data type.

Start_location

Start to search for the character position of expression1 in expression2. If start_location is not specified and is a negative number or zero, the search starts from the beginning of espression2. Start_location can be of the bigint type.
3: return type

If the data type of expression2 is varchar (max), nvarchar (max), or varbinnary (max), it is bigint; otherwise, it is int.

4: Remarks

If expression1 cannot be found in expression2, charindex returns 0.

Charindex performs the comparison operation based on the input sorting rules. To compare with a specified sorting rule, you can use collate to apply the explicit sorting rule to the input value.

The return start position starts from 1, not from 0.

5: Example

Select charindex ('A', 'badf', 1): 2

Select charindex ('A', 'badf', 0): 2

Select charindex ('A', 'badf', 2): 2

Select charindex ('A', 'badf', 3): 0

Select charindex ('G', 'badf', 3): 0



Ii. substring

1. Syntax

Substring (expression, start, length );

2. Parameters

Expression

Is a string, a second-level string, text, image, column, or an expression that contains a column. Do not use expressions that contain aggregate functions.

Start

An integer that specifies the start position of a string. Start can be of the bigint type.

Length

An integer that specifies the number of characters or bytes of the expression to be returned. If length is negative, an error is returned. Length can be of the bigint type.

3. Remarks

The offset (start and length) of the ntext, char, or varchar data type must be specified by the number of characters ). The offset of data types such as text, image, binary, and varbinary must be specified by the number of bytes.

4. Example:

Select substring ('gxs ',-1, 3): G

Select substring ('gxs ',-1, 2): NULL

Select substring ('gxs ', 0, 1): NULL

Select substring ('gxs ', 1, 1): G

Select substring ('gxs ', 1, 2): GX

Combined Use of SubString and CharIndex

Select substring ('a, B, C, D', 0, charindex (',', 'a, B, C, D', 0 ))





**************************************** * Recommended Villa drawings *********************************** **************





I want to have a villa, facing the sea, spring blossoms


Longxing technology villa design drawings, here are the latest and most comprehensive Villa drawings,


Here are the most powerful Villa drawing discounts, including the construction diagram, structure diagram, water supply and drainage diagram, electrical Diagram


We are committed to providing villa design drawings to our customers. The drawings are designed by professional and experienced design teams. We provide free construction technical guidance.


Address: http://bstzsj.taobao.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.