Introduction to SQL function substr, SQL function substr
Interpretation of the substr (string, 1, 3) function: returns a string with a length of three characters from left to right.
Result: str
From right to left?
There should be another function to provide such a function!
In fact, the function from right to left is just a different parameter.
Interpretation of the substr (String,-) function: Take the first digit on the right of the string and start taking three digits.
Result: g
Substr (string, start, length)
String-specifies the string to be truncated.
Start-required, specifying where the string starts. Positive number-start at the specified position of the string, negative number-start at the specified position from the end of the string, and 0-Start at the first character of the string.
Length-(optional) specifies the length of the string to be truncated. If the length is short, all characters before the end of the character expression value are returned.
For example, select substr ('abcdefg', 3,4) from dual; the result is cdef.
Select substr ('abcdefg',-3,4) from dual; Result efg
Note: The first position in the string is always 1. The query results of the following two SQL statements are the same:
For example: select substr ('abcdefg',) from dual; the result is abc.
Select substr ('abcdefg',) from dual; the result is abc.
Difference from substring in java class:
Substring (int beginIndex, int endIndex): starts from the specified beginIndex and ends at the specified endIndex-1. The first position in the string is always 0. EndIndex (Optional). It returns the characters starting from the specified index until the end of the string.
Example: "unhappy". substring (2) returns "happy"
"Hamburger". substring (3,8) returns "burge"
The above is all the content of this article. I hope you will like it.