0

I want to use part of a string in a where statement, the issue is to be able to correctly extract the ending part of the string.

example of the string is below

Token=hdhddjdjdjjkfdjkfkjdfu743895390458059035ufjfjfjfdjkdsur0934r9rjdjdsjsdkk&value=CRT&SaveAndContinue=

I want to get "SaveAndContinue" and for the example below, I want to return "SaveAndContinue"

Token=hdhdhdb.sjcdchwuhv;ovhvjvdbkdsjskdsj372893984024fhewjsac01239301913ru32rcbjs&email=ShshtwuidS%hdhdhdhsjkweuydjdj.COM&confirmation=true&SaveAndContinue=
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
icenature
  • 39
  • 4

3 Answers3

0

You may use substring function and this regexp &([^&=]+)=$ i.e. pick characters except '&' and '=' after the last '&' until the '=' before the end of string.

select substring('hdhddjdjdjjkfdjkfkjdfu743895390458059035ufjfjfjfdjkdsur0934r9rjdjdsjsdkk&value=CRT&SaveAndContinue='
                 from '&([^$=]+)=$');
select substring('hdhdhdb.sjcdchwuhv;ovhvjvdbkdsjskdsj372893984024fhewjsac01239301913ru32rcbjs&email=ShshtwuidS%hdhdhdhsjkweuydjdj.COM&confirmation=true&SaveAndContinue='
                 from '&([^&=]+)=$');

Both return 'SaveAndContinue'.

DB-fiddle

Stefanov.sm
  • 11,215
  • 2
  • 21
  • 21
0

You could split it, which works with Postgres 14 and higher, but all string function cost time

CREATE TABLE my_table ( a text)
CREATE TABLE
INSERT INTO my_table VALUES ('Token=hdhddjdjdjjkfdjkfkjdfu743895390458059035ufjfjfjfdjkdsur0934r9rjdjdsjsdkk&value=CRT&SaveAndContinue='),
('Token=hdhdhdb.sjcdchwuhv;ovhvjvdbkdsjskdsj372893984024fhewjsac01239301913ru32rcbjs&email=ShshtwuidS%hdhdhdhsjkweuydjdj.COM&confirmation=true&SaveAndContinue=')

INSERT 0 2
SELECT  trim(trailing '=' from split_part(a, '&', -1))
  FROM my_table;
rtrim
SaveAndContinue
SaveAndContinue
SELECT 2

fiddle

nbk
  • 45,398
  • 8
  • 30
  • 47
0

The task is not well defined, but assuming that you want to get the data between last & and = (both should be present) then you can try regexp_extract:

-- sample data
WITH dataset (str) AS (
    values ('Token=hdhddjdjdjjkfdjkfkjdfu743895390458059035ufjfjfjfdjkdsur0934r9rjdjdsjsdkk&value=CRT&SaveAndContinue=')
)

-- query
select regexp_extract(str, '&([^$=]+)=[^=]*$', 1)
from dataset;

Output:

     _col0      
-----------------
 SaveAndContinue 
Guru Stron
  • 102,774
  • 10
  • 95
  • 132