Loading...

verticapy.sql.functions.substr#

verticapy.sql.functions.substr(expr: str | list[str] | StringSQL | list[StringSQL], position: int, extent: int | None = None) StringSQL#

Returns VARCHAR or VARBINARY value representing a substring of a specified string.

Parameters#

expr: SQLExpression

Expression.

position: int

Starting position of the substring.

extent: int, optional

Length of the substring to extract.

Returns#

StringSQL

SQL string.

Examples#

First, let’s import the vDataFrame in order to create a dummy dataset.

from verticapy import vDataFrame

Now, let’s import the VerticaPy SQL functions.

import verticapy.sql.functions as vpf

We can now build a dummy dataset.

df = vDataFrame({"x": ["Badr", "Colin", "Fouad", "Arash"]})

Now, let’s go ahead and apply the function.

df["substr_x"] = vpf.substr(df["x"], 1, 1)
display(df)
Abc
x
Varchar(5)
100%
Abc
substr_x
Varchar(4)
100%
1BadrB
2ColinC
3FouadF
4ArashA

Note

It’s crucial to utilize VerticaPy SQL functions in coding, as they can be updated over time with new syntax. While SQL functions typically remain stable, they may vary across platforms or versions. VerticaPy effectively manages these changes, a task not achievable with pure SQL.

See also

vDataFrame.eval() : Evaluates the expression.