The `SUBSTR` function, a commonly used string manipulation function in SQL, is present in both DB2 and Oracle databases with similar purposes but slight differences in syntax and parameters. This function is designed to extract a substring from a given string, starting at a specified position and, optionally, for a specified length.
DB2 Syntax:
In DB2, the `SUBSTR` function's basic syntax is as follows:
SUBSTR(string, start, length)
- `string` is the source string from which the substring will be extracted.
- `start` is the position in the string where extraction will begin. The first position in the string is 1.
- `length` is an optional parameter that specifies the number of characters to be extracted. If omitted, the substring from the start position to the end of the string is returned.
Oracle Syntax:
In Oracle, the `SUBSTR` function shares a similar syntax:
SUBSTR(string, start, length)
- `string` refers to the input string from which the substring will be extracted.
- `start` indicates the position in the string from which the extraction should begin. A positive value starts the extraction at the specified position counting from the beginning of the string, while a negative value counts from the end of the string.
- `length` is also an optional parameter in Oracle and specifies the number of characters to extract. If not specified, the substring from the start position to the end of the string is extracted.
Key Differences:
While the basic syntax and functionality of the `SUBSTR` function are consistent between DB2 and Oracle, there are nuances and extended features that may differ:
- Negative Start Position: Oracle's `SUBSTR` function uniquely allows the `start` parameter to be negative, which means the substring extraction can begin from the end of the string, moving backwards. This feature is not standard in DB2's implementation of `SUBSTR`.
- Function Overloading and Variants: Both DB2 and Oracle might offer variations and overloads of the `SUBSTR` function, catering to specific data types or offering additional functionality. These variants can include differences in handling different character sets, bytes versus characters for multibyte character languages, and specific versions tailored for binary data.
- Error Handling and Behavior: The way errors or edge cases are handled might differ slightly between the two databases. For example, how each database handles cases where the start position is greater than the string length, or the specified length extends beyond the end of the string, may vary.
In conclusion, while the core functionality and basic syntax of the `SUBSTR` function in DB2 and Oracle are largely similar, it is essential for users to be mindful of database-specific behaviors, especially concerning negative start positions and function variants. Understanding these subtleties ensures the accurate and effective use of the `SUBSTR` function in database operations and string manipulation tasks within each respective environment.
SUBSTR is used for string manipulation with column name, first position and string length used as arguments. For example