Need regex for SQL to extract substring from a long string - sql

I have the below string in my database (SnowFlake)
:display_redemption_mark: 0
:redeemable_properties: '1205,1206,1207,1208,1209,1210,1211,1212,1213,1214,1215,1216,1217,1218,1225,1227,1230,1231,1232,1239,1242,1244,1249,1250,1254,1281,1282,1283,1284,1285,1286,1287,1288,1289,1290,1291,1292,1293,1294,1296,1297,1240,1247,1255'
:redemption_meta_keys: ''
:max_redemptions_in_transaction: 0
:allow_multiple_redemption_on_item: false
:allow_qualifying_items_reused: false
I want to extract the value of the redeemable_properties which in above case
1205,1206,1207,1208,1209,1210,1211,1212,1213,1214,1215,1216,1217,1218,1225,1227,1230,1231,1232,1239,1242,1244,1249,1250,1254,1281,1282,1283,1284,1285,1286,1287,1288,1289,1290,1291,1292,1293,1294,1296,1297,1240,1247,1255
I want to exclude single quote, and double quote as the whole value is a string and is stored in a column of the table. I am creating a new column with the extracted value.

Can you try this one?
select trim( regexp_substr( your_column, '.*:redeemable_properties:([^:]*):',1,1,'e' ), '''" ' ) from your_table;

Related

Oracle replace some duplicated characters (non digits )

anyone can help me to build proper syntax for regexp_replace to remove any multiplicated non-digits and non-letters from string ? If digit/letter is multiplicated - it is not changed
eg.
source and expected result:
'ABBC000001223, ABC00000212,,, '
'ABBC000001223, ABC00000212, '
(removed second occurance of space after comma and second and third comma )
Use this REGEXP_REPLACE to match any non alphanumeric character in the first group
([^[:alnum:]])
followed by one or more same charcters (group 1)
([^[:alnum:]])(\1)+
and replace it with the original character (group 1)
I added some other data to demonstrate the result
with dta as (
select 'ABBC000001223, ABC00000212,,, ' txt from dual union all
select ',.,;,;;;;,,,,,,,,,,,,#''++`´' txt from dual union all
select 'ABBC000001223ABC00000212' txt from dual)
select txt,
regexp_replace(txt,'([^[:alnum:]])(\1)+', '\1') result
from dta
TXT
-------------------------------
RESULT
--------------------------------
ABBC000001223, ABC00000212,,,
ABBC000001223, ABC00000212,
,.,;,;;;;,,,,,,,,,,,,#'++`´
,.,;,;,#'+`´
ABBC000001223ABC00000212
ABBC000001223ABC00000212

Trimming empty string or spaces in SQL

What happens when you try and trim an empty string or a bunch of spaces in SQL Server? Does it become null or ''?
trimming will only leave you with a blank space.
ltrim(rtrim(' '))
but
nullif(ltrim(rtrim(' ')),'') will give you null
Here's an easy way to test string manipulations, manually throw some strings together and find out:
;with cte as (SELECT ' 0 ' AS col
UNION SELECT ' ' AS col
)
SELECT LTRIM(RTRIM(col)) as col
FROM cte
Which returns a zero and a blank string. To get a NULL you'd have to use NULLIF()
I run this command while updating all row.
Revise your need.
For example:
UPDATE TableName SET ColumnName = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(ColumnName, CHAR(10), CHAR(32)), CHAR(13), CHAR(32)), CHAR(160), CHAR(32)),CHAR(9),CHAR(32))))

split a string on multiple delimiters

I have a sql that returns the example string below:
Input PKIND:BCMOX:10048301-
output BCMOX:10048301
I need to write code the first substring the string on - then split it on : and return the 2 & 3 item (BCMOX:10048301)
If the string format is consistent and you want to extract everything after the first : until the first occurrence of -, use a combination of substr and instr.
select substr(col, instr(col,':')+1, instr(col,'-')-instr(col,':')-1)
from yourtable
where instr(col,':') > 0 and instr(col,'-') > 0 --to get the rows which have these 2 characters
The REGEXP_SUBSTR version. Return everything between the first colon and the first hyphen.
select regexp_substr('PKIND:BCMOX:10048301-', ':(.*)-', 1, 1, NULL, 1) from dual;

How to use regexp_substr() with group of delimiter characters?

I have a string something like this 'SERO02~~~NA_#ERO5'. I need to sub string it using delimiter ~~~. So can get SERO02 and NA_#ERO5 as result.
I create an regex experession like this:
select regexp_substr('SERO02~~~NA_#ERO5' ,'[^~~~]+',1,2) from dual;
It worked fine and returns : NA_#ERO5
But if I change the string to ERO02~NA_#ERO5 the result is still same.
But I expect the expression to return nothing since delimiter ~~~ is not found in that string. Can someone help me out to create correct expression?
[^~~~] matches a single character that is not one of the characters following the caret in the square brackets. Since all those characters are identical then [^~~~] is the same as [^~].
You can match it using:
SELECT REGEXP_SUBSTR(
'SERO02~~~NA_#ERO5',
'~~~(.*?)(~~~|$)',
1,
1,
NULL,
1
)
FROM DUAL;
Which will match ~~~ then store zero-or-more characters in a capture group (the round brackets () indicates a capture group) until it finds either ~~~ or the end-of-string. It will then return the first capture group.
You can do it without regular expressions, with a bit of logics:
with test(text) as ( select 'SERO02~~~NA_#ERO5' from dual)
select case
when instr(text, '~~~') != 0 then
substr(text, instr(text, '~~~') + 3)
else
null
end
from test
This will give the part of the string after '~~~', if it exists, null otherwise.
You can edit the ELSE part to get what you need when the input string does not contain '~~~'.
Even using regexp,to match the string '~~~', you need to write it exactly, without []; the [] is used to list a set of characters, so [aaaaa] is exactly the same than [a],while [abc] means 'a' OR 'b' OR 'c'.
With regexp, even if not necessary, one way could be the following:
substr(regexp_substr(text, '~~~.*'), 4)
In case you want all elements. Handles NULL elements too:
SQL> with tbl(str) as (
select 'SERO02~~~NA_#ERO5' from dual
)
select regexp_substr(str, '(.*?)(~~~|$)', 1, level, null, 1) element
from tbl
connect by level <= regexp_count(str, '~~~') + 1;
ELEMENT
-----------------
SERO02
NA_#ERO5
SQL>

find comma in oracle sql string

I have a column in my oracle db as character and data stored in this is like here
30.170527093355002,72.615875338654 and
30.805165,71.82474
Now I want to get the separated by comma whole string. I mean I want to get the part of string before comma and also part after comma separately. Please any one tell me is there any built in function to do this that I can separate my while string by comma regardless of comma position where it exist.I have already tried floor function and substr but all in vain please help me to use any built in function or user defined function to full fill my requirements.
select
substr( COLNAME, 1, instr( COLNAME, ',') - 1 ) as p_1 ,
substr( COLNAME, instr( COLNAME, ',', - 1 ) + 1 ) as p_2
from YOURTABLE