The location where SQL Server obtains the last character.

Source: Internet
Author: User

The location where SQL Server obtains the last character.

CHARINDEX ('characters', reverse (field name ))

This means to reverse the field, that is, to retrieve the last position of a character from the back to the front.


Obtain the final position of a string using SQL

Declare @ str varchar (50)
Set @ str = '1-9-3-2'
Select REVERSE (SUBSTRING (REVERSE (@ str), 1, CHARINDEX ('-', REVERSE (@ str)-1 ))

How can I retrieve the right character of the last special character in a string, for example, 10*20*300? How can I get 300?

DECLARE @ aa VARCHAR (30)
SET @ aa = '10*20*300'
-- Take a fixed-length string, such as the 3 characters After *
Select substring (@ aa, LEN (@ aa)-CHARINDEX ('*', REVERSE (@ aa) + 2, 3)
-- Or take all the characters after the last *
Select right (@ aa, CHARINDEX ('*', REVERSE (@ aa)-1)

-- The result is 300.

Related Article

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.