SUBSTR
The SUBSTR function in SQL is used to extract a substring from a string.
Syntax
SUBSTR(string, start_position, length)
Parameters
string: The original string.start_position: The position in the string where the extraction of the substring will begin. The position of the first character is 1. Ifstart_positionis 0 or negative, the function treats it as 1.length(optional): The number of characters to extract. Iflengthis not included, the function will return all characters starting fromstart_position.
Return Value
The function returns a string which is a substring of the original string. The substring starts at start_position and has length number of characters.
Errors
- If the
stringparameter is not a string value, aEvaluateError::FunctionRequiresStringValueerror will be returned. - If the
start_positionorlengthparameters are not integer values, aEvaluateError::FunctionRequiresIntegerValueerror will be returned. - If the
lengthparameter is negative, aEvaluateError::NegativeSubstrLenNotAllowederror will be returned.
Examples
Consider a table Item:
You can use the SUBSTR function to get a substring from the name values:
SELECT SUBSTR(name, 2) AS test FROM Item;
This will return:
lop mc blee
(empty string)
teven the &long named$ folken!
The function takes the substring starting from the second character until the end for each name value.