Functions on Strings
There are various built-in functions on strings. In any string, position starts at 0 and ends at length - 1.
If you want to follow along with the examples, download the script baggageschema_loaddata.sql and execute it as shown below. This script creates the table used in the example and loads data into the table.
java -jar lib/kvstore.jar kvlite -secure-config disable
java -jar lib/sql.jar -helper-hosts localhost:5000 -store kvstoreload command, execute the
script.load -file baggageschema_loaddata.sqlsubstring function
substring function extracts a string from a given string
according to a given numeric starting position and a given numeric substring
length.
returnvalue substring (source, position [, substring_length] )
source ::= any*
position ::= integer*
substring_length ::= integer*
returnvalue ::= stringSELECT substring(bag.baginfo.routing,0,3) AS Source
FROM baggageInfo bag
WHERE ticketNo=1762376407826{"Source":"JFK"}concat function
concat function concatenates all its arguments and displays the
concatenated string as
output.returnvalue concat (source,[source*])
source ::= any*
returnvalue ::= stringSELECT concat("The route for passenger ",fullName , " is ", bag.baginfo[0].routing)
FROM baggageInfo bag
WHERE ticketNo=1762376407826{"Column_1":"The route for passenger Dierdre Amador is JFK/MAD"}upper and lower functions
upper and lower are simple functions to
convert to fully upper case or lower case respectively. The
upper function converts all the characters in a
string to uppercase. Thelower function converts all the
characters in a string to
lowercase.returnvalue upper (source)
returnvalue lower (source)
source ::= any*
returnvalue ::= stringSELECT upper(fullname) AS FULLNAME_CAPITALS
FROM BaggageInfo
WHERE ticketNo=1762376407826{"FULLNAME_CAPITALS":"DIERDRE AMADOR"}SELECT lower(fullname) AS fullname_lowercase
FROM BaggageInfo WHERE ticketNo=1762376407826{"fullname_lowercase":"dierdre amador"}trim function
trim function enables you to trim leading or trailing
characters (or both) from a string. The ltrim function enables you to
trim leading characters from a string. The rtrim function enables you
to trim trailing characters from a
string.returnvalue trim(source [, position [, trim_character]])
source ::= any*
position ::= "leading"|"trailing"|"both"
trim_character ::= string*
returnvalue ::= stringreturnvalue ltrim(source)
returnvalue rtrim(source)
source ::= any*
returnvalue ::= stringSELECT trim(bag.baginfo[0].routing,"trailing"," ")
FROM BaggageInfo bag
WHERE ticketNo=1762376407826{"Column_1":"JFK/MAD"}ltrim function to remove leading
spaces:SELECT ltrim(bag.baginfo[0].routing)
FROM BaggageInfo bag
WHERE ticketNo=1762376407826{"Column_1":"JFK/MAD"}rtrim function to remove trailing
spaces:SELECT rtrim(bag.baginfo[0].routing)
FROM BaggageInfo bag
WHERE ticketNo=1762376407826{"Column_1":"JFK/MAD"}length function
length function returns the length of a character string. The
length function calculates the length using the UTF character
set.returnvalue length(source)
source ::= any*
returnvalue ::= integerSELECT fullname, length(fullname) AS fullname_length
FROM BaggageInfo
WHERE ticketNo=1762350390409{"fullname":"Fallon Clements","fullname_length":15}contains function
contains function indicates whether or not a search string is
present inside the source
string.returnvalue contains(source, search_string)
source ::= any*
search_string ::= any*
returnvalue ::= booleanSELECT fullname FROM baggageInfo bag
WHERE EXISTS bag.bagInfo[contains($element.routing,"SFO")]{"fullname":"Michelle Payne"}
{"fullname":"Lucinda Beckman"}
{"fullname":"Henry Jenkins"}
{"fullname":"Lorenzo Phil"}
{"fullname":"Gerard Greene"}starts_with and ends_with functions
starts_with function indicates whether or not the source string
begins with the search
string.returnvalue starts_with(source, search_string)
source ::= any*
search_string ::= any*
returnvalue ::= booleanends_withfunction indicates whether or not the source
string ends with the search
string.returnvalue ends_with(source, search_string)
source ::= any*
search_string ::= any*
returnvalue ::= booleanSELECT $flightLeg.flightNo,
$flightLeg.actions[contains($element.actionCode, "Checkin")].actionTime AS checkinTime,
$flightLeg.actions[contains($element.actionCode, "BagTag Scan")].actionTime AS bagScanTime,
timestamp_diff(
$flightLeg.actions[contains($element.actionCode, "Checkin")].actionTime,
$flightLeg.actions[contains($element.actionCode, "BagTag Scan")].actionTime
) AS diff
FROM baggageinfo $s, $s.bagInfo[].flightLegs[] AS $flightLeg
WHERE ticketNo=176234463813
AND starts_with($s.bagInfo[].routing, $flightLeg.fltRouteSrc)Explanation: In the baggage data, every flightLeg has an actions
array. There are three different actions in the actions array. The action code for the
first element in the array is Checkin/Offload. For the first leg, the action code is
Checkin and for the other legs, the action code is Offload at the hop. The action code
for the second element of the array is BagTag Scan. In the query above, you determine
the difference in action time between the bag tag scan and check-in time. You use the
contains function to filter the action time only if the action code
is Checkin or BagScan. Since only the first flight leg has details of check-in and bag
scan, you additionally filter the data using starts_with function to
fetch only the source code fltRouteSrc.
{"flightNo":"BM572","checkinTime":"2019-03-02T03:28:00Z",
"bagScanTime":"2019-03-02T04:52:00Z","diff":-5040000}SELECT fullname FROM baggageInfo $bagInfo
WHERE ends_with($bagInfo.bagInfo[].routing, "JTR"){"fullname":"Lucinda Beckman"}
{"fullname":"Gerard Greene"}
{"fullname":"Michelle Payne"}index_of function
index_of function determines the position of the first
character of the search string at its first occurrence if
any.returnvalue index_of(source, search_string [, start_position])
source ::= any*
search_string ::= any*
start_position ::= integer*
returnvalue ::= integer- Returns the position of the first character of the search string at its first occurrence. The position is relative to the start position of the string (which is zero).
- Returns -1 if
search_stringis not present in the source. - Returns 0 for any value of source if the
search_stringis of length 0. - Returns NULL if any argument is NULL.
- Returns NULL if any argument is an empty sequence or a sequence with more than one item.
- Returns error if
start_positionargument is not an integer.
SELECT index_of(bag.baginfo.flightLegs[0].estimatedArrival,"-")
FROM BaggageInfo bag
WHERE ticketNo=1762320569757{"Column_1":4}SELECT index_of(bag.baginfo.routing,"/")
FROM BaggageInfo bag
WHERE ticketNo=1762320569757"Column_1":3}replace function
replace function returns the source with every occurrence of
the search string replaced with the replacement string.
returnvalue replace(source, search_string [, replacement_string])
source ::= any*
search_string ::= any*
replacement_string ::= any*
returnvalue ::= stringSELECT replace(bag.bagInfo[0].routing,"SFO","SOF")
FROM baggageInfo bag
WHERE ticketNo=1762320569757{"Column_1":"SOF/IST/ATH/JTR"}Example 2: Replace the double quote in the passenger name with a single quote.
SELECT fullname,
replace(fullname, "\"", "'") as new_fullname
FROM BaggageInfo bagreverse function
reverse function returns the characters of the source string in
reverse order, where the string is written beginning with the last character
first.returnvalue reverse(source)
source ::= any*
returnvalue ::= stringSELECT fullname, reverse(fullname)
FROM baggageInfo
WHERE ticketNo=1762330498104{"fullname":"Michelle Payne","Column_2":"enyaP ellehciM"}