Oracle SUBSTRB() Function
The Oracle (PL/SQL) SUBSTRB() function is used to extract a substring from a string starting from specified position. This function calculates length of the substring using bytes instead of characters of the input string.
Syntax
SUBSTRB(string, start, length)
Parameters
string |
Required. Specify the string to extract from. It can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. |
start |
Required. Specify the start position. It can be both a positive or negative number.
|
length |
Optional. Specify the length of the substring to extract. If omitted, the whole string will be returned (from the start position). If it is less than 1, the function returns null. |
Return Value
Returns the substring extracted from specified string.
Example 1:
The example below shows the usage of SUBSTRB() function.
SUBSTRB('AlphaCodingSkills.com', 1) Result: 'AlphaCodingSkills.com' SUBSTRB('AlphaCodingSkills.com', 6) Result: 'CodingSkills.com' SUBSTRB('AlphaCodingSkills.com', 6, 6) Result: 'Coding' SUBSTRB('AlphaCodingSkills.com', -4, 4) Result: '.com'
Example 2:
Consider a database table called Employee with the following records:
PhoneNumber | EmpID | Address |
---|---|---|
+33-147996101 | 1 | Grenelle, Paris, France |
+31-201150319 | 2 | Geuzenveld, Amsterdam, Netherlands |
+86-1099732458 | 3 | Yizhuangzhen, Beijing, China |
+65-67234824 | 4 | Yishun, Singapore |
+81-357799072 | 5 | Koto City, Tokyo, Japan |
In the query below, the SUBSTRB() function is used to extract the country code from the PhoneNumber column records.
SELECT Employee.*, SUBSTRB(PhoneNumber, 2, 2) AS CountryCode FROM Employee;
This will produce the result as shown below:
PhoneNumber | EmpID | Address | CountryCode |
---|---|---|---|
+33-147996101 | 1 | Grenelle, Paris, France | 33 |
+31-201150319 | 2 | Geuzenveld, Amsterdam, Netherlands | 31 |
+86-1099732458 | 3 | Yizhuangzhen, Beijing, China | 86 |
+65-67234824 | 4 | Yishun, Singapore | 65 |
+81-357799072 | 5 | Koto City, Tokyo, Japan | 81 |
❮ Oracle Functions