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!