I have a string that looks something similar to this:
28f1e5f7-47c6-4d67-bcbf-9e807c379076-9780-gThGFkHY0CeFCPwA6Efys7
I would like to split it based on '-' but also tell it at what position or point to do the split. My goal is to drop the last two strings
The end result of what I want should look something like this:
28f1e5f7-47c6-4d67-bcbf-9e807c379076
I know i can split offset and then concat. I'm wondering if there is a more straight forward way around as it will require me to split 5 times and then concatenate 5 times?
Can you try this Query?
with myTable as (
select 1 as id, "28f1e5f7-47c6-4d67-bcbf-9e807c379076-9780-gThGFkHY0CeFCPwA6Efys7" as sampleString
union all select 2 as id, "28f1e5f7-47c6-4d67-bcbf-9e807c379076-9780-gThGFkHY0CeFCPwA6Efys7-dhushkdha" as sampleString
)
SELECT id, sampleString,
(
SELECT STRING_AGG(samp, '-' ORDER BY index)
FROM UNNEST(SPLIT(sampleString, '-')) samp WITH OFFSET index
WHERE index BETWEEN 0 AND (select array_length(split(sampleString,'-')) - 3)
)
FROM myTable
This query will omit the last two values given that it is delimited by "-"
Output:
I added anonther value to the 2nd column just to test if will delete the last two values.
You might consider using INSTR() as well
INSTR(source_value, search_value[, position[, occurrence]])
WITH sample_data AS (
SELECT '28f1e5f7-47c6-4d67-bcbf-9e807c379076-9780-gThGFkHY0CeFCPwA6Efys7' str
)
SELECT SUBSTR(str, 1, INSTR(str, '-', -1, 2) - 1) -- drop the last two strings
FROM sample_data;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.