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. TheKColumnorKTableColumnthat invokes the method will be the one supplied to theLENGTHfunction.length(KEncoding encoding): Receives aKEncodingwhich will be supplied to theLENGTHfunction. TheKColumnorKTableColumnthat invokes the method will be the one supplied to theLENGTHfunction.
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 aKColumnor aKTableColumnwhich will be supplied to theLENGTHfunction.length(KValTextField kValTextField): Receives aKValTextFieldwhich will be supplied to theLENGTHfunction.length(KColumn kColumn, KEncoding encoding): Receives aKColumnor aKTableColumnand aKEncodingwhich will be supplied to theLENGTHfunction.length(KValTextField kValTextField, KEncoding encoding): Receives aKValTextFieldand aKEncodingwhich will be supplied to theLENGTHfunction.
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"