Brightlane's logging system extracts the message body from log entries that begin with a fixed 8-character prefix.
Write a query to return everything from position 9 onward in the string 'ERROR: Connection refused'.
Output:
- A single row with one column,
message_body, containing the suffix from position9to the end of the string.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
SUBSTRING('ERROR: Connection refused', 9) AS message_body The shape
SUBSTRING('ERROR: Connection refused', 9) returns everything from position 9 to the end of the string. With no third argument, SUBSTRING takes the source from the start position onward instead of slicing a fixed-width window, which is exactly what extracting a variable-length message body needs.
Clause by clause
SELECT SUBSTRING('ERROR: Connection refused', 9) AS message_bodyextracts the suffix and labels itmessage_body. The two-argument form is the source string and the start position: position1is theEofERROR, position7is the colon, positions7and8are the two spaces in the prefix, and position9is theCofConnection. The function returns every character from there to the finald, which is the message body'Connection refused'.- There is no
FROMbecause the source is the literal in theSELECT. The function operates on one input and returns one output.
Why omit the length
The message body has no fixed width. 'Connection refused' is 18 characters; another log entry might carry 'Disk full' at 9 characters or a long stack trace at hundreds. Passing a specific length to SUBSTRING would either truncate the longer messages or trail spaces on the shorter ones. The two-argument form takes everything to the end of the string by definition, which is the only shape that handles every input correctly.
You practiced SUBSTRING(string, start) without a length — return everything from the start position to the end of the string.