An example of the usage of SUBSTRING functions in SQL Server _mssql

Source: Internet
Author: User

The substring function in SQL is used to grab a portion of a field data. The name of this function is not exactly the same in different repositories:

    • Mysql:substr (), SUBSTRING ()
    • Oracle:substr ()
    • SQL server:substring ()

The substring function in SQL is used to intercept a portion of a field data.

For example, we need to extract the ' Abd ' from the string ' Abdcsef ', which can be implemented using substring:

select substring('abdcsef',1,3)

Results:

'abd'

The number ' 1 ' in parentheses indicates that the starting position of the interception begins with the first character of the string, and that ' 3 ' indicates that the length of the string obtained after the interception is 3 characters.

This is the most basic grammar of ' substring ', of course, our needs sometimes become more complex, such as the following example:

We just want the room number in ' Roomno ' and find that the starting character position is not fixed, and the length of the room number we need is not fixed.

At this point, we can use the ' charindex ' function to easily handle, which is used to locate a particular character in the string position, that is, the function

The resulting result is a number that represents the position of a particular character. Execute the following code:

Select Room_stand=substring (Roomno,charindex, Roomno) +1,charindex (' Room ', Roomno)-charindex (' Yuan ', Roomno)-1) 
From Property_room

Results:

The above is a small set of SQL Server to introduce the use of the SUBSTRING Function example analysis, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.