SQL Tutorial SQL Advanced SQL Database SQL References

Oracle SUBSTRC() Function



The Oracle (PL/SQL) SUBSTRC() function is used to extract a substring from a string starting from specified position. This function calculates length of the substring using Unicode complete characters.

Syntax

SUBSTRC(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.
  • If it is a positive number, this function extracts from the beginning of the string.
  • If it is a negative number, this function extracts from the end of the string.
  • If it is 0, then it is treated as 1.
Note that the first position in string starts with 1.
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 SUBSTRC() function.

SUBSTRC('AlphaCodingSkills.com', 1)
Result: 'AlphaCodingSkills.com'

SUBSTRC('AlphaCodingSkills.com', 6)
Result: 'CodingSkills.com'

SUBSTRC('AlphaCodingSkills.com', 6, 6)
Result: 'Coding'

SUBSTRC('AlphaCodingSkills.com', -4, 4)
Result: '.com'

Example 2:

Consider a database table called Employee with the following records:

PhoneNumberEmpIDAddress
+33-1479961011Grenelle, Paris, France
+31-2011503192Geuzenveld, Amsterdam, Netherlands
+86-10997324583Yizhuangzhen, Beijing, China
+65-672348244Yishun, Singapore
+81-3577990725Koto City, Tokyo, Japan

In the query below, the SUBSTRC() function is used to extract the country code from the PhoneNumber column records.

SELECT Employee.*, 
SUBSTRC(PhoneNumber, 2, 2) AS CountryCode 
FROM Employee;

This will produce the result as shown below:

PhoneNumberEmpIDAddressCountryCode
+33-1479961011Grenelle, Paris, France33
+31-2011503192Geuzenveld, Amsterdam, Netherlands31
+86-10997324583Yizhuangzhen, Beijing, China86
+65-672348244Yishun, Singapore65
+81-3577990725Koto City, Tokyo, Japan81

❮ Oracle Functions