presto Get the string before/after the specified character

In Presto, you can use the split_part function to split a string according to a specified delimiter. The specific syntax is as follows:

split_part(string, delimiter, index)

Among them, string indicates the string to be intercepted, delimiter indicates the delimiter, and index indicates the serial number of the substring to be returned after being divided by the delimiter, and the serial number starts from 1. For example, if you want to separate the string hello,world,presto by comma, and get the second substring world, you can use the following SQL statement:

SELECT split_part('爱,开,发', ',', 2);

The execution result is:

 open

In addition, if you want to intercept a certain part of the string, you can use the substr function. The specific syntax is as follows:

substr(string, start, length)

Among them, string indicates the string to be intercepted, start indicates the starting position to be intercepted, and length indicates the length to be intercepted. For example, if you want to intercept the last 5 characters starting from the 7th character of the string hello,world,presto, you can use the following SQL statement:

SELECT substr('hello,world,presto', 7, 5);

The execution result is:

world

It should be noted that if the length to be truncated exceeds the length of the string, the substr function will truncate to the end of the string.

Guess you like

Origin blog.csdn.net/X8i0Bev/article/details/129543226