Length
Definition
The length
method allows you to add the LENGTH
function to the query. The LENGTH
function is used to count the number of characters of a string. Additionally, you can add an encoding to this function and it will count the number of characters of a string in the given encoding. The string must be valid in this encoding.
There are 2 ways to call this method:
1. Calling from a KColumn
or a KTableColumn
The methods available to use this functionality calling from a KColumn
or a KTableColumn
are:
length()
: It does not receive any parameters. TheKColumn
orKTableColumn
that invokes the method will be the one supplied to theLENGTH
function.length(KEncoding encoding)
: Receives aKEncoding
which will be supplied to theLENGTH
function. TheKColumn
orKTableColumn
that invokes the method will be the one supplied to theLENGTH
function.
Example: ()
Java code:
k
.select(
APP_USER.EMAIL.length()
)
.from(APP_USER)
.multiple();
SQL generated:
SELECT
LENGTH(au.email)
FROM app_user au
Parameters:
- None
Example: (KEncoding)
Java code:
k
.select(
APP_USER.EMAIL.cast(bytea()).length(utf8())
)
.from(APP_USER)
.multiple();
SQL generated:
SELECT
LENGTH(CAST(au.email AS BYTEA), 'UTF8')
FROM app_user au
Parameters:
- None
2. Calling from the KFunction
class
The methods available to use this functionality calling from the KFunction
class are:
length(KColumn kColumn)
: Receives aKColumn
or aKTableColumn
which will be supplied to theLENGTH
function.length(KValTextField kValTextField)
: Receives aKValTextField
which will be supplied to theLENGTH
function.length(KColumn kColumn, KEncoding encoding)
: Receives aKColumn
or aKTableColumn
and aKEncoding
which will be supplied to theLENGTH
function.length(KValTextField kValTextField, KEncoding encoding)
: Receives aKValTextField
and aKEncoding
which will be supplied to theLENGTH
function.
To use this way, you need to import the static functions as follows:
import static com.myzlab.k.KFunction.*;
Example: (KColumn)
Java code:
k
.select(
length(APP_USER.EMAIL)
)
.from(APP_USER)
.multiple();
SQL generated:
SELECT
LENGTH(au.email)
FROM app_user au
Parameters:
- None
Example: (KValTextField)
Java code:
k
.select(
length(val("A short text"))
)
.single();
SQL generated:
SELECT LENGTH(?1)
Parameters:
- ?1: "A short text"
Example: (KColumn, KEncoding)
Java code:
k
.select(
length(APP_USER.EMAIL.cast(bytea()), utf8())
)
.from(APP_USER)
.multiple();
SQL generated:
SELECT
LENGTH(CAST(au.email AS BYTEA), 'UTF8')
FROM app_user au
Parameters:
- None
Example: (KValTextField, KEncoding)
Java code:
k
.select(
length(val("a short text").cast(bytea()), utf8())
)
.single();
SQL generated:
SELECT LENGTH(CAST(?1 AS BYTEA), 'UTF8')
Parameters:
- ?1: "A short text"