SQL charindex usage

Source: Internet
Author: User

Select * From table_a where charindex ('A', ID)> 0 or charindex ('B', ID)> 0
The ID field in Table _ A contains "A" or "B" records.

---------------------------------------------------
Charindex
Returns the starting position of the specified expression in the string.
Syntax
Charindex (expression1, expression2 [, start_location])
Parameters
Expression1
An expression that contains the order of characters to be searched. Expression1 is a short character data type classification expression.
Expression2
An expression is usually a column used to search for a specified sequence. Expression2 belongs to the string data type classification.
Start_location
Search for the start character position of expression1 in expression2. If start_location is not specified but is a negative number or zero, the search starts from the start position of expression2.
Return type
Int

Note
If either expression1 or expression2 belongs to the Unicode data type (nvarchar or nchar), the other is converted to the Unicode data type.
If either expression1 or expression2 is null, charindex returns NULL when the database compatibility level is 70 or greater. If the database compatibility level is 65 or more hours, charindex returns NULL only when both expression1 and expression2 are null.
If expression1 is not found in expression2, charindex returns 0.

Example
FirstCodeReturn the position where the sequence "wonderful" starts in the Notes column of the titles table. The second example uses the optional start_location parameter to search for "wonderful" starting from the fifth character in the Notes column ". The third example shows the result set when expression1 cannot be found in expression2.

Use pubs
Go
Select charindex ('Wonderful ', notes)
From titles
Where title_id = 'tc3218'
Go

-- Use the optional start_location parameter to start searching
-- For wonderful starting with the specified th character in the Notes
-- Column.
Use pubs
Go
Select charindex ('Wonderful ', notes, 5)
From titles
Where title_id = 'tc3218'
Go

The following is the result set of the first query and the second query:
-----------
46
(1 row (s) affected)
Use pubs
Go
Select charindex ('wondrous', notes)
From titles
Where title_id = 'tc3218'
Go

The following is the result set.

-----------
0

Address: http://wangqiaowqo.javaeye.com/blog/525750

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.