Find and Replace a strings in oracle - sql

I have a string which contains data as follows:
'ID,MASTER_ID,DATA_SOURCE_ID,DATA_SOURCE_NAME,CHILD_COUNT,CHILD_COUNT_TEXT,PARENT_OR_CHILD,RECORD_TYPE,FULL_NAME_LNF,FULL_NAME_FNF,FIRST_NAME,LAST_NAME,PREFIX,SUFFIX,MIDDLE,TITLE,NAME_OF_ORGANIZATION,NAME_OF_BUSINESS,TYPE_OF_ENTITY,ADDRESS,CITY,STATE,PROVINCE,POSTAL_CODE,COUNTRY,POSTAL_ADDRESS_TYPE,PHONE_AREA_CODE,PHONE_NUMBER,PHONE_COUNTRY_CODE,PHONE,PHONE_TYPE,EMAIL_ADDRESS,URL,HCP_SPECIALTY,HCP_TYPE,HCP_SUBTYPE,RECIPIENT_STATUS,COVERED_RECIPIENT_FLAG,RELATIONSHIP_TO_CR,LAST_MODIFIED_BY,LAST_MODIFIED_DATE,PRIMARY_LICENSE_STATE_AR,PRIMARY_LICENSE_NUM_AR,DEA_REG_NUM_AR,NPI_NUM_AR,UPIN_AR,TAX_PAYER_ID_AR,PRIMARY_LICENSE_STATE_CR,PRIMARY_LICENSE_NUM_CR,DEA_REG_NUM_CR,NPI_NUM_CR,UPIN_CR,DEA_NUMBER,NPI,UPIN,TIN,TAX_PAYER_ID_CR,ATTRIBUTE13,ATTRIBUTE14,ATTRIBUTE15,ATTRIBUTE16,ATTRIBUTE17,ATTRIBUTE18,ATTRIBUTE19,ATTRIBUTE20,ATTRIBUTE21,ATTRIBUTE22,ATTRIBUTE23,ATTRIBUTE24,ATTRIBUTE25,ATTRIBUTE26,ATTRIBUTE27,ATTRIBUTE28,ATTRIBUTE29,ATTRIBUTE30,SOURCE_REGION_CODE,SOURCE_SYSTEM_CODE,REC_INVALID_FLAG,REVISION_FLAG,IS_ACTIVE,PROCESS_STATE,RECIPIENT_CATEGORY01,RECIPIENT_CATEGORY02,RECIPIENT_CATEGORY03,RECIPIENT_CATEGORY04,RECIPIENT_CATEGORY05,RECIPIENT_CATEGORY06,RECIPIENT_CATEGORY07,RECIPIENT_CATEGORY08,RECIPIENT_CATEGORY09,RECIPIENT_CATEGORY10,RECIPIENT_CATEGORY11,RECIPIENT_CATEGORY12,RECIPIENT_CATEGORY13,RECIPIENT_CATEGORY14,RECIPIENT_CATEGORY15,RECIPIENT_CATEGORY16,RECIPIENT_CATEGORY17,RECIPIENT_CATEGORY18,RECIPIENT_CATEGORY19,RECIPIENT_CATEGORY20,RECIPIENT_CATEGORY21,RECIPIENT_CATEGORY22,RECIPIENT_CATEGORY23,RECIPIENT_CATEGORY24,RECIPIENT_CATEGORY25,RECIPIENT_CATEGORY26,RECIPIENT_CATEGORY27,RECIPIENT_CATEGORY28,RECIPIENT_CATEGORY29,RECIPIENT_CATEGORY30,IS_PICKABLE,IS_GOLDEN,PRIMARY_LICENSE_NUM,PRIMARY_LICENSE_EFFECTIVE,PRIMARY_LICENSE_EXPIRES,TERTIARY_LICENSE_EFFECTIVE,TERTIARY_LICENSE_EXPIRES,SECONDARY_LICENSE_EFFECTIVE,SECONDARY_LICENSE_EXPIRES,SECONDARY_LICENSE_NUM,TERTIARY_LICENSE_NUM,ADDRESS2,PHONE_AREA_CODE2,PHONE_NUMBER2,PHONE_COUNTRY_CODE2,PHONE_TYPE2,TERRITORY,PRIMARY_AFFILIATION,PRIMARY_AFFILIATION_STATE,REQUEST_WF_STATE,IS_EDIT_LOCKED,SOURCE_SYSTEM_RECIPIENT_ID,CREATED_BY,CREATION_DATE,APPROVER_COMMENTS,SECONDARY_LICENSE_STATE,PRIMARY_LICENSE_STATE,NPI_DATA,STATE_DATA,DEA_DATA,RPPS,FINESS,SIREN_NUMBER,DPC'
Can anybody tell me how to find only the following values
DATA_SOURCE_ID,
LAST_MODIFIED_BY,
LAST_MODIFIED_DATE,
ATTRIBUTE13,
ATTRIBUTE14,
ATTRIBUTE15,
ATTRIBUTE16,
ATTRIBUTE17,
ATTRIBUTE18,
ATTRIBUTE19,
ATTRIBUTE20,
ATTRIBUTE21,
ATTRIBUTE22,
ATTRIBUTE23,
ATTRIBUTE24,
ATTRIBUTE25,
ATTRIBUTE26,
ATTRIBUTE27,
ATTRIBUTE28,
ATTRIBUTE29,
ATTRIBUTE30,
And then replace them with following strings
'L.DATA_SOURCE_ID,L.LAST_MODIFIED_BY,L.LAST_MODIFIED_DATE,L.ATTRIBUTE13,L.ATTRIBUTE14,L.ATTRIBUTE15,L.ATTRIBUTE16,L.ATTRIBUTE17,L.ATTRIBUTE18,L.ATTRIBUTE19,L.ATTRIBUTE20,L.ATTRIBUTE21,L.ATTRIBUTE22,L.ATTRIBUTE23,L.ATTRIBUTE24,L.ATTRIBUTE25,L.ATTRIBUTE26,L.ATTRIBUTE27,L.ATTRIBUTE28,L.ATTRIBUTE29,L.ATTRIBUTE30,'

You are looking for this. It gives you your desired result
SELECT mycol, CONCAT('L.', mycol) AS newCol
FROM(SELECT * FROM test WHERE mycol REGEXP
'DATA_SOURCE_ID|LAST_MODIFIED_BY|LAST_MODIFIED_DATE|ATTRIBUTE[0-9]+')
as temp
EXPLANATION
SELECT * FROM test WHERE mycol REGEXP
'DATA_SOURCE_ID|LAST_MODIFIED_BY|LAST_MODIFIED_DATE|ATTRIBUTE[0-9]+')
This finds all rows that either have DATA_SOURCE_ID or LAST_MODIFIED_BY or LAST_MODIFIED_DATE or ATTRIBUTE followed by any number
SELECT mycol, CONCAT('L.', mycol) AS newCol
This adds L. to all the rows that has been found by the subquery.
OUTPUT
L.DATA_SOURCE_ID,
L.LAST_MODIFIED_BY,
L.LAST_MODIFIED_DATE,
L.ATTRIBUTE13,
L.ATTRIBUTE14,
L.ATTRIBUTE15,
L.ATTRIBUTE16,
L.ATTRIBUTE17,
L.ATTRIBUTE18,
L.ATTRIBUTE19,
L.ATTRIBUTE20,
L.ATTRIBUTE21,
L.ATTRIBUTE22,
L.ATTRIBUTE23,
L.ATTRIBUTE24,
L.ATTRIBUTE25,
L.ATTRIBUTE26,
L.ATTRIBUTE27,
L.ATTRIBUTE28,
L.ATTRIBUTE29,
L.ATTRIBUTE30
Hope this helps

You can try like this:
replace( replace(myString, 'DATA_SOURCE_ID', 'yourReplaceValue1'), 'LAST_MODIFIED_BY', yourReplaceValu2).....
This ..... is for you to make the similar replace for the rest of your replacing strings :)

Use nested REPLACE:
SQL> WITH data AS
2 ( SELECT 'a,b,c' str FROM dual
3 )
4 SELECT str,
5 REPLACE(REPLACE(REPLACE(str, 'a', 'x'), 'y'), 'z') new_str
6 FROM data;
STR NEW_S
----- -----
a,b,c x,b,c

Related

Oracle replace all characters before "." dot

I need to replace all characters with nothing before the . character and also replace all [ and ] with nothing.
Please see examples below:
from
to
[PINWHEEL_ASSET].[MX5530]
MX5530
[PINWHEEL_TRADE].[AR5403]
AR5403
The parts before and after the . dot are variables.
with
sample_data (my_string) as (
select '[PINWHEEL_ASSET].[MX5530]' from dual
)
select rtrim(substr(my_string, instr(my_string, '.') + 2), ']') as second_part
from sample_data
;
SECOND_PART
-----------
MX5530
This assumes that the input string looks exactly like this: [first].[second], where "first" and "second" are (possibly empty) strings that do not contain periods or closing brackets.
Yet another option is to use regular expressions (see line #6).
Sample data:
SQL> with test (col) as
2 (select '[PINWHEEL_ASSET].[MX5530]' from dual union all
3 select '[PINWHEEL_TRADE].[AR5403]' from dual
4 )
Query begins here:
5 select col,
6 regexp_substr(col, '\w+', 1, 2) result
7 from test;
COL RESULT
------------------------- --------------------
[PINWHEEL_ASSET].[MX5530] MX5530
[PINWHEEL_TRADE].[AR5403] AR5403
SQL>

How to get first string after character Oracle SQL

I'm trying to get first string after a character.
Example is like
ABCDEF||GHJ||WERT
I need only
GHJ
I tried to use REGEXP but i couldnt do it.
Can anyone help me with please?
Thank you
Somewhat simpler:
SQL> select regexp_substr('ABCDEF||GHJ||WERT', '\w+', 1, 2) result from dual;
^
RES |
--- give me the 2nd "word"
GHJ
SQL>
which reads as: give me the 2nd word out of that string. Won't work properly if GHJ consists of several words (but that's not what your example suggests).
Something like I interpret with a separator in place, In this case it is || or | example is with oracle database
-- pattern -- > [^] represents non-matching character and + for says one or more character followed by ||
-- 3rd parameter --> starting position
-- 4th parameter --> nth occurrence
WITH tbl(str) AS
(SELECT 'ABCDEF||GHJ||WERT' str FROM dual)
SELECT regexp_substr(str
,'[^||]+'
,1
,2) output
FROM tbl;
I think the most general solution is:
WITH tbl(str) AS (
SELECT 'ABCDEF||GHJ||WERT' str FROM dual UNION ALL
SELECT 'ABC|DEF||GHJ||WERT' str FROM dual UNION ALL
SELECT 'ABClDEF||GHJ||WERT' str FROM dual
)
SELECT regexp_replace(str, '^.*\|\|(.*)\|\|.*', '\1')
FROM tbl;
Note that this works even if the individual elements contain punctuation or a single vertical bar -- which the other solutions do not. Here is a comparison.
Presumably, the double vertical bar is being used for maximum flexibility.
You should use regexp_substr function
select regexp_substr('ABCDEF||GHJ||WERT ', '\|{2}([^|]+)', 1, 1, 'i', 1) str
from dual;
STR
---
GHJ

Extract Value from a string PostgreSQL

Simple Question
I have the following type of results in a string field
'Number=123456'
'Number=1234567'
'Number=12345678'
How do I extract the value from the string with regard that the value can change between 5-8 figures
So far I did this but I doubt that fits my requirement
SELECT substring('Size' from 8 for ....
If I can tell it to start from the = sign till the end that would help!
The likely simplest solution is to trim 7 leading characters with right():
right(str, -7)
Demo:
SELECT str, right(str, -7)
FROM (
VALUES ('Number=123456')
, ('Number=1234567')
, ('Number=12345678')
) t(str);
str | right
-----------------+----------
Number=123456 | 123456
Number=1234567 | 1234567
Number=12345678 | 12345678
You could use REPLACE:
SELECT col, REPLACE(col, 'Number=', '')
FROM tab;
DBFiddle Demo
Based on this question:
Split comma separated column data into additional columns
You could probably do the following:
SELECT *, split_part(col, '=', 2)
FROM table;
You may use regexp_matches :
with t(str) as
(
select 'Number=123456' union all
select 'Number=1234567' union all
select 'Number=12345678' union all
select 'Number=12345678x9'
)
select t.str as "String",
regexp_matches(t.str, '=([A-Za-z0-9]+)', 'g') as "Number"
from t;
String Number
-------------- ---------
Number=123456 123456
Number=1234567 1234567
Number=12345678 12345678
Number=12345678x9 12345678x9
--> the last line shows only we look chars after equal sign even if non-digit
Rextester Demo

Avoid nested regexp_replace( ) for changing multiple char(s) in a string

I want to replace multiple char(s) in a string using a simple query for Oracle 11g. Now I use:
select regexp_replace(regexp_replace(regexp_replace(colName, 'a', 'x'), 'b', 'y' ) 'c', 'z') from myTable;
but I'm not very happy with it. What would be a more intuitive query that has the same output?
The translate function will accomplish what you want:
select translate(colName, 'abc', 'xyz') from myTable;
translate() searches the string in the first argument for characters in the second argument and replaces them with the characters found in the third argument that are in the same position as those in the second argument:
SQL> select translate('abc', 'abc', 'xyz')
from dual;
TRA
---
xyz
SQL> select translate('tralalajustbecause','abc','xyz') from dual;
TRA
---
trxlxlxjustyezxuse

Insert character between string Oracle SQL

I need to insert character string after each character in Oracle SQL.
Example:
ABC will A,B,C
DEFG will be D,E,F,G
This question gives only one character in string
Oracle insert character into a string
Edit: As some fellows have mentioned, Oracle does not admit this regex. So my approach would be to do a regex to match all characters, add them a comma after the character and then removing the last comma.
WITH regex AS (SELECT REGEXP_REPLACE('ABC', '(.)', '\1,') as reg FROM dual) SELECT SUBSTR(reg, 1, length(reg)-1) FROM regex;
Note that with the solution of rtrim there could be errors if the string you want to parse has a final ending comma and you don't want to remove it.
Previous solution: (Not working on Oracle)
Check if this does the trick:
SELECT REGEXP_REPLACE('ABC', '(.)(?!$)', '\1,') FROM dual;
It does a regexp_replace of every character, but the last one for the same character followed by a ,
To see how regexp_replace works I recommend you: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions130.htm
SELECT rtrim(REGEXP_REPLACE('ABC', '(.)', '\1,'),',') "REGEXP_REPLACE" FROM dual;
You could do it using:
REGEXP_REPLACE
RTRIM
For example,
SQL> WITH sample_data AS(
2 SELECT 'ABC' str FROM dual UNION ALL
3 SELECT 'DEFG' str FROM dual UNION ALL
4 SELECT 'XYZ' str FROM dual
5 )
6 -- end of sample_data mimicking a real table
7 SELECT str,
8 rtrim(regexp_replace(str, '(\w?)', '\1,'),',') new_str
9 FROM sample_data;
STR NEW_STR
---- ----------
ABC A,B,C
DEFG D,E,F,G
XYZ X,Y,Z
Since there is no way to negate the end of string in an Oracle regex (that does not support lookarounds), you may use
SELECT REGEXP_REPLACE(
REGEXP_REPLACE('ABC', '([^,])([^,])','\1,\2'),
'([^,])([^,])',
'\1,\2')
AS Result from dual
See the DB Fiddle. The point here is to use REGEXP_REPLACE with ([^,])([^,]) pattern twice to cater for consecutive matches.
The ([^,])([^,]) pattern matches any non-comma char into Group 1 (\1) and then any non-comma char into Group 2 (\2), and inserts a comma in between them.