Index
Often, in our applications, we need to work with text strings, and DB2 SQL can come in very useful and simplify our code. In this guide we see some interesting SQL functions for string manipulation:
This function returns the starting position of a character or string within another string, for example if I look for “only” within the string “This is only an example” I expect a “9” as a result… simple I would say!
select posstr('This is only an example', 'only') from sysibm.sysdummy1; -- Result 9
In a concrete example, we could use the POSSTR function to “pars” a CSV:
-- (1) We simulate a table with a string like CSV
CREATE or replace TABLE
qtemp.my_csv_file
(csvstring varchar(300));
--(2) Let's populate the table with some examples
INSERT INTO qtemp.my_csv_file (csvstring)
VALUES('Christine; Hass')
, ('Micheal; Thompson')
, ('Roberto;De Pedrini');
--(3) We read the normal content
SELECT csvstring from qtemp.my_csv_file;
-- Result
CSVSTRING file
Christine; Hass
Micheal; Thompson
Roberto;De Pedrini
;
--(4) We pars the CSV
SELECT substr(csvstring, 1, POSSTR(csvstring, ';') -1) AS FirstName,
substr(csvstring, POSSTR(csvstring, ';') (AS LastName
FROM qtemp.my_csv_file;
-- Result
FIRSTNAME LASTNAME
Christine Hass
Micheal Thompson
Roberto De Pedrini
When things get complicated and we have to look for one string inside another starting from a certain location LOCATE and LOCATE_IN_STRING are more useful than the POSSTR function just seen: but beware of the syntax of these scalar function… parameters are reversed this time compared to before.
Let’s start with a simple example … we look for the string “this” starting from position 10 of a second string:
select locate('this', 'this is only an example, but this time we can start everywhere', 10)
from sysibm.sysdummy1;
-- Result
30
Starting with the qTEMP table created in the previous step, this time we use the LOCATE function to achieve a similar result:
SELECT substr(csvstring, 1, locate(';', csvstring,1)-1) AS FirstName
,substr(csvstring, locate(';', csvstring, 1)'1) aS LastNam
eFROM qtemp.my_csv_file
;--Result
FIRSTNAME LASTNAME
Christine Hass
Micheal Thompson
Roberto De Pedrini
When the going gets tough… LOCATE_IN_STRING starts playing!
You like to win easy with a CSV of just two items! Let’s complicate our lives with a slightly more real example. attention… the parameters of the scalar funtion are reversed another time … thank you SQL for always making our life easy (“you kill them!’).
-- (1) Let's simulate a table with a string like C
SVCREATE or replace TAB
LE qtemp.my_csv_f
ile(csvstring varchar(3
00));--(2) Let's populate the table with some
examples INSERT INTO qtemp.my_csv_file (
csvstring) VALUES('Christine; Ha
ss; A01; Press'),('Micheal; Thom
pson; A02; Manager'),('Roberto;De P
edrini; A03; Big Boss');--(3) Let'
s read the normal contentSELECT csvstr
ing from qte
mp.my_cs
v_file;-- ResultCSVSTRI
NGChristine; Hass; A01; Pre
ssMicheal; Thompson; A02; Mana
g
erRoberto;De Pedrini:A03; Big Bo
ss;--(4) We pars the CSVSELECT locate_in_string (csvs
tring, ';'1,1) as Primo, locate_in_string (csvs
tring, ';'1,2) as Second,locate_in_string (c
svstring, ';'1,3) as t
hirdFROM qtemp.my_csv_file; SELECT substr(csvstring, 1, locate_in_string(csvstr
ing, ';'1,1)-1) AS FirstName,substr(csvstring, locate_in_string(csvstring, ';',1,1)-1, locate_in_string (csvstring, ';'1,2)-locate_in_string (c
svstring, ';'1,1)-1) to LastName,substr ""csvstring", locate_in_string (csvstring, 'csvstring,'1,1,2)-1, locate_in_string (csvstring, '1,3)-locat
e_in_string (csvstring, ';',1,2)-1) aS Workdepth,substr(csvstr
ing, locate_in_string
(csvstrin
g, ';'1,3)-1) aS JobFROM qtemp.my_csv_
file ResultFIRSTNAME LASTNAME WORKDEPTH
JOBChristine Hass A01 PressMicheal Thompso
n A02 ManagerRoberto De Pedrini A03 Big Boss
Three simple scalar functions for string manipulation… but much more awaits us!
We are pleased to receive and share this "tip & trick" from Patrick Rizzi, which introduces a technique that allows…
I take inspiration from a response by Michael Mayer on the Midrange.com mailing lists to someone who asked how to…
Businesses are increasingly seeking tools to enhance efficiency, collaboration, and resource management. Enterprise Resource Planning (ERP) systems provide a comprehensive…
Early April saw the release of the "Spring Version" of ACS Access Client Solution, version 1.1.9.5 Interesting new features especially…
If the packed agenda of sessions at Common Europe Congress 2024, June 3-6 Milan, wasn't enough for you, here's another…
Debugging functions with Visual Studio Code have been available for some time but this new version 2.10.0 simplifies the handling…
View Comments