SLICE¶
The SLICE statement is a function in GlueSQL that allows you to retrieve a subsection of a list. It is analogous to slicing operations in many programming languages.
Syntax¶
column_name: Name of the column containing the list.start_index: The starting index from where the slice should begin. This value can be negative.length: The number of elements to be included in the slice.
Examples¶
Consider the following table Test:
With the following data:
1. Basic Slicing¶
Retrieve the first 2 elements from a list.
Result:
2. Slicing Beyond List Length¶
If the combined start index and length exceed the list size, SLICE will return all possible elements without error.
Result:
3. Start Index Beyond List Length¶
If the start index alone exceeds the list size, SLICE will return an empty list.
Result:
4. Using Negative Start Index¶
A negative start index counts from the end of the list.
Result:
Another example of a negative start index.
Result:
If the absolute value of the negative start index exceeds the list length, it is treated as index 0.
Result:
Errors¶
- Using a non-list value for slicing will result in an error:
ListTypeRequired. - Using a non-integer value for the start index or length will result in an error:
FunctionRequiresIntegerValue("SLICE").