SQL Server Substring function usage instance parsing, serversubstring

Source: Internet
Author: User
Tags sql server substring

SQL Server Substring function usage instance parsing, serversubstring

The substring function in SQL is used to capture part of a column. The function names are not exactly the same in different databases:

  • MySQL: SUBSTR (), SUBSTRING ()
  • Oracle: SUBSTR ()
  • SQL Server: SUBSTRING ()

The substring function in SQL is used to extract part of a column.

For example, if we need to extract 'abd' from the string 'abdcsef', substring can be used for implementation:

select substring('abdcsef',1,3)

Result:

'abd'

The number '1' in parentheses indicates that the starting position of the truncation starts from the first character of the string, and '3' indicates that the string length after the truncation is 3 characters.

This is the most basic syntax of 'string'. Of course, our requirements are sometimes complicated. For example:

We only want to get the room number in 'roomno' and find that the starting character is not fixed, and the length of the room number we need is not fixed.

In this case, we can use the 'charindex' function to easily locate the position of a specific character in the string, that is, the function

The result is a number that represents a specific character position. Run the following code:

Select room_stand = substring (roomno, charindex ('meta', roomno) + 1, charindex ('room', roomno)-charindex ('meta', roomno)-1) from PROPERTY_roomwhere roomno like '% unit % room %'

Result:

The above is an example of how to use the Substring function in SQL Server. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.