SPLIT_PART
Split a character string where a delimiter string falls, then return one part of the string according to its numeric position.
SPLIT_PART(string, delimiter, number)
For example, the following string has five parts delimited by commas:
10,20,30,40,50
The first part of this string is the value 10
, the second is 20
, and so on.
In the following example, the query splits the ftscore
column on the hyphen character (-
), then returns the values on either side as home and away goals:
premdb=# SELECT ftscore, SPLIT_PART(ftscore,'-',1) homegoals, SPLIT_PART(ftscore,'-',2) awaygoals
FROM match LIMIT 5;
ftscore | homegoals | awaygoals
---------+-----------+-----------
0-1 | 0 | 1
0-1 | 0 | 1
2-1 | 2 | 1
3-0 | 3 | 0
3-0 | 3 | 0
(5 rows)